Jwt-auth: Type error: Argument 1 passed to Tymon\JWTAuth\JWT::fromUser() must be an instance of Tymon\JWTAuth\Contracts\JWTSubject, instance of App\User given

Created on 7 Apr 2017  路  12Comments  路  Source: tymondesigns/jwt-auth

Type error: Argument 1 passed to Tymon\JWTAuth\JWT::fromUser() must be an instance of Tymon\JWTAuth\Contracts\JWTSubject, instance of App\User given

Most helpful comment

came here because of this error... what now?

edit: my bad, i forgot the: "implements JWTSubject" at User Model

class User extends Authenticatable implements JWTSubject

All 12 comments

Hi there, i was having the same problem...

Reading along all the issues.... I found this #860.

Pretty nice tutorial... will save your day!

you need to update the User Model.

Check the link
http://jwt-auth.readthedocs.io/en/docs/quick-start/#update-your-user-model

@sivakumarraju89 ,
I have updated User Model, however the same problem persists.

This works like a breeze!

http://jwt-auth.readthedocs.io/en/docs/quick-start/#update-your-user-model

DAMN! Suddenly it does not work anymore! ERROR!

"message": "Call to undefined method Illuminate\Auth\TokenGuard::once()",

came here because of this error... what now?

edit: my bad, i forgot the: "implements JWTSubject" at User Model

class User extends Authenticatable implements JWTSubject

@leandroruel
after doing that

Interface 'App\JWTSubject' not found

@Illusionist3886
You should also import JWTSubject like so:

use Tymon\JWTAuth\Contracts\JWTSubject;

I had to do this as well.
https://africalocals.com/?/question/375

In case it has gone ...

add the folowing 2 methods in your User model

/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey();
}

/**
 * Return a key value array, containing any custom claims to be added to the JWT.
 *
 * @return array
 */
public function getJWTCustomClaims()
{
    return [];
}

came here because of this error... what now?

edit: my bad, i forgot the: "implements JWTSubject" at User Model

class User extends Authenticatable implements JWTSubject

Work for me
Laravel version 5.7.28
"tymon/jwt-auth": "~1.0.0-rc.2"

For Laravel above 5.4
in my case i user laravel 7

Install JWT package via composer

composer require tymon/jwt-auth

Publish the config

php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"

Generate secret key

php artisan jwt:secret

Configure Auth guard inside the config/auth.php

'defaults' => [
'guard' => 'api', #use api as guard
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],

    'api' => [
        'driver' => 'jwt',  #use jwt in the driver
        'provider' => 'users',
        'hash' => false,
    ],
],

Update your User model

  use Tymon\JWTAuth\Contracts\JWTSubject;

and add implements as this

   class User extends Authenticatable implements JWTSubject

then add those in the user model

public function getJWTIdentifier()
{
return $this->getKey();
}

/**
 * Return a key value array, containing any custom claims to be added to the JWT.
 *
 * @return array
 */
public function getJWTCustomClaims()
{
    return [];
}

that's all hope this helps

For Laravel above 5.4
in my case i user laravel 7

Install JWT package via composer

composer require tymon/jwt-auth

Publish the config

php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"

Generate secret key

php artisan jwt:secret

Configure Auth guard inside the config/auth.php

'defaults' => [
'guard' => 'api', #use api as guard
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],

    'api' => [
        'driver' => 'jwt',  #use jwt in the driver
        'provider' => 'users',
        'hash' => false,
    ],
],

Update your User model

  use Tymon\JWTAuth\Contracts\JWTSubject;

and add implements as this

   class User extends Authenticatable implements JWTSubject

then add those in the user model

public function getJWTIdentifier()
{
return $this->getKey();
}

/**
 * Return a key value array, containing any custom claims to be added to the JWT.
 *
 * @return array
 */
public function getJWTCustomClaims()
{
    return [];
}

that's all hope this helps

this is helpful to me. Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

harveyslash picture harveyslash  路  3Comments

loic-lopez picture loic-lopez  路  3Comments

marciomansur picture marciomansur  路  3Comments

shah-newaz picture shah-newaz  路  3Comments

Rasoul-Karimi picture Rasoul-Karimi  路  3Comments