tymondesigns / jwt-auth

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

Custom model auth not generated token #2171

Open caktoy opened 2 years ago

caktoy commented 2 years ago

Subject of the issue

I'm using my own auth model, fully different with laravel's built-in user model, but using Authenticable by laravel. When I try to login by that user, I cannot generate the token, $token = auth()->login($user);

Your environment

Q A
Bug? no
New Feature? no
Framework Laravel
Framework version ^8.0
Package version ^1.0
PHP version 7.3.28

Steps to reproduce

Note: the credentials of my auth model is using Username and Passwd column. Auth model:

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class MUser extends Authenticatable implements JWTSubject
{
    use Notifiable;

    protected $table = 'muser';

    protected $primaryKey = 'UserID';

    protected $hidden = ['Passwd'];
}

Auth config:

[
    ...
   'guards' => [
       'api' => [
           'driver' => 'jwt',
           'provider' => 'muser',
           'hash' => false,
       ]
   ],
   'providers' => [
        ...
        'muser' => [
            'driver' => 'eloquent',
            'model' => App\Models\MUser::class,
        ],
    ],
]

In controller:

$user = MUser::where('Username', request()->username)->firstOrFail();
// my own password validation method...
$token = auth()->login($user);

Expected behaviour

Token generated

Actual behaviour

null generated