Type error: Argument 1 passed to Tymon\JWTAuth\JWT::fromUser() must be an instance of Tymon\JWTAuth\Contracts\JWTSubject, instance of App\User given
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
composer require tymon/jwt-auth
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"
php artisan jwt:secret
'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,
],
],
use Tymon\JWTAuth\Contracts\JWTSubject;
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 7Install 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 JWTSubjectthen 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
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