PHP-Open-Source-Saver / jwt-auth

🔐 JSON Web Token Authentication for Laravel & Lumen
MIT License
729 stars 113 forks source link

User failed to transfer to token #195

Closed fengerwoo closed 1 year ago

fengerwoo commented 2 years ago

Subject of the issue

User failed to transfer to token

Your environment:

Q A
Bug? yes
New Feature? no
Framework Laravel
Framework version 9.33.0
Package version 2.0.0
PHP version 8.1.9

Steps to reproduce

 auth()->login($user);

The user failed to transfer to token. The specific error is reported

Lcobucci\JWT\Token\Builder::relatedTo(): Argument #1 ($subject) must be of type string, null given, called in vendor/php-open-source-saver/jwt-auth/src/Providers/JWT/Lcobucci.php on line 212 in vendor/psy/psysh/src/Exception/TypeErrorException.php on line 53.

Operation screenshot

User Model implements are also correct

Messhias commented 2 years ago

Question: did that happen outside the thinker environment too (eg. Web)?

fengerwoo commented 2 years ago

Question: did that happen outside the thinker environment too (eg. Web)?

@Messhias Yes, so does the web

fengerwoo commented 2 years ago

@Messhias Change the getJWTCustomClaims in the User Model to the following, and the token can be obtained, but I still don't know what the root problem is. When I used tymon/jwt-auth before, getJWTCustomClaims can return an empty array here

public function getJWTCustomClaims(){
        return [ 'sub' => $this->id, ];
}

Modify screenshot

Messhias commented 2 years ago

@Messhias Change the getJWTCustomClaims in the User Model to the following, and the token can be obtained, but I still don't know what the root problem is. When I used tymon/jwt-auth before, getJWTCustomClaims can return an empty array here

public function getJWTCustomClaims(){
        return [ 'sub' => $this->id, ];
}

Modify screenshot

I did same as you and worked as well, @eschricker did that happens to you too?

eschricker commented 2 years ago

I don't need to pass ['sub' => $this->id] as custom claim.

I can't reproduce this error.

Messhias commented 1 year ago

I tried to reproduce the error again and didn't happen, what could be happened it's the $subject it wasn't a string.