tymondesigns / jwt-auth

🔐 JSON Web Token Authentication for Laravel & Lumen
https://jwt-auth.com
MIT License
11.27k stars 1.55k forks source link

Type error: Argument 1 passed to Tymon\JWTAuth\JWTGuard::login() must be an instance of Tymon\JWTAuth\Contracts\JWTSubject, instance of App\User given #1379

Open ricardo-lobo opened 6 years ago

ricardo-lobo commented 6 years ago

I tried to follow the unfinished docs for 1.0.*

When I try to login I always get this error:

Type error: Argument 1 passed to Tymon\JWTAuth\JWTGuard::login() must be an instance of Tymon\JWTAuth\Contracts\JWTSubject, instance of App\User given, called in /Users/ricardolobo/Sites/backend/vendor/tymon/jwt-auth/src/JWTGuard.php on line 124

I'm using Laravel 5.5 and PHP 7.1

ricardo-lobo commented 6 years ago

I "solved" this by removing JWTSubject type hinting on the following files:

vendor/tymon/jwt-auth/src/JWT.php vendor/tymon/jwt-auth/src/JWTGuard.php

rafaelrenanpacheco commented 6 years ago

@ricardo-lobo did you read the quick-start guide?

http://jwt-auth.readthedocs.io/en/docs/quick-start/

There's an item called "Update your User model" with the following code:

class User extends Authenticatable implements JWTSubject

This should solve that error you got.

gregorskii commented 6 years ago

Make sure if you move your User model into say App\Models\User that you update the config/auth model:

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],

I made this mistake... few hours of debugging. :)

ferdousulhaque commented 6 years ago

Usually it is due to Tymon\JWTAuth\Contracts\JWTSubject which needs to be added in User Model. Also as JWTSubject implements in the User model, JWTSubject methods must be declared inside the model as below. After that BINGO !! you will get token as expected.

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

public function getJWTCustomClaims()
{
    return [];
}
dontee01 commented 6 years ago

I have a separate table(not users table) handling merchant authentication for my app. So I'm using laravel query builder. Here's the error I got

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

zinkyawmoe commented 3 years ago

did you implements JWTSubject in User Model? class User extends Authenticatable implements JWTSubject

rickibaadinugraha commented 3 years ago

I "solved" this by removing JWTSubject type hinting on the following files:

vendor/tymon/jwt-auth/src/JWT.php vendor/tymon/jwt-auth/src/JWTGuard.php

thanks,i take it and look solve problem good job

Kallou0 commented 2 years ago

@rafaelrenanpacheco's solution worked for me class User extends Authenticatable implements JWTSubject

vivek-1445 commented 2 years ago

Replace from : 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, ],

    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],
],

TO,

'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ],

    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],
],

It's Work for me.

lexm25 commented 2 years ago

Hi, i solved this with the comment of the person who said "Did you read the quick-start guide?". I ilterally copy this "class User extends Authenticatable implements JWTSubject"(the extend part is the important thing) into my User model class and it solved the problem, ty.

ajaymarathe commented 1 year ago

@ricardo-lobo did you read the quick-start guide?

http://jwt-auth.readthedocs.io/en/docs/quick-start/

There's an item called "Update your User model" with the following code:

class User extends Authenticatable implements JWTSubject

This should solve that error you got.

It's working now, 💯 thanks 👍🏻