Jwt-auth: Returns ["user_not_found"] on authentication with user

Created on 17 Jun 2015  路  16Comments  路  Source: tymondesigns/jwt-auth

After generating a token on user registration and using it to authenticate user,returns this: ["user_not_found"]

i am using laravel 5.1

Most helpful comment

This actually worked for me. $authuser = JWTAuth::toUser(JWTAuth::getToken());. I still passed the token through Authorization header. But this $user = JWTAuth::parseToken()->authenticate(); as found in the documentation did not work.

All 16 comments

@TrustOkoroego try passing the token as a parameter in your url. Looks like it's not picked at the moment when passed via the authorization header

What code are you using to generate the token?

JWTAuth::fromUser()

This actually worked for me. $authuser = JWTAuth::toUser(JWTAuth::getToken());. I still passed the token through Authorization header. But this $user = JWTAuth::parseToken()->authenticate(); as found in the documentation did not work.

IDK if this will help anyone else out but I was having a similar issue and stumbled on this thread.
I had to set the identifier because we use unorthodox naming conventions.

JWTAuth::setIdentifier('user_id');

It was looking for an _id_ field so it would not set the _sub_ header

don't forget you can set this in the config ;)

@tymondesigns Yes I did forget that. Or neglected to notice it when I was originally scanning it.
Thanks for pointing that out.

Thanks. I got the same error. but, fixed it by overriding the $primaryKey variable with my custom db field in my user class like,
File: app\Models\User.php
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
...
/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'userID';

@cvinothkumar

protected $primaryKey = 'userID';

works for me,tks.

Hi,
i ran into the same problem, i generate my token using
public function login(Request $request)
{
$user = Auth::user();
return $token = JWTAuth::fromUser($user);
}
when i pass this generated token in url it allway's return
{"error":"user_not_found"}

i had this problem as well with laravel 5.1.

@TrustOkoroego's solution fixed it for me. I am not using a custom primary key.

@phpnets that's the answer if you're using a primary key other than 'id'. I had this problem still with 'id'.

same issue here @PsychicCat did you solve it

I have the same issue because i use 2 users model separate for web and api, it tooks me 2 days to figure out why. This because jwt always use default guard was set in config/auth.php. In my situation default guards is web so when authenticate with jwt , although i was set user model in jwt config, jwt still use the model provider for web was set in config/auth.php.Here is what i'm doing:

  • config/jwt.php:
    'user' => 'App\Models\AppUser',
    'identifier' => 'id',
    - create a new middleware: app\Http\Middleware\GetUserFromToken.php and copy content from Tymon\JWTAuth\Middleware\GetUserFromToken then add this line to the first line of handle function:
    config(['auth.defaults.guard' => 'api']);
  • config/auth.php:
    'guards' => [
    'web' => [
    'driver' => 'session',
    'provider' => 'users',
    ],
    'api' => [
    'driver' => 'session',
    'provider' => 'app_users',
    ],

    ],

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

],
I'm using laravel 5.3
Hope it helpful!

@phpnets that's the answer if you're using a primary key other than 'id'. I had this problem still with 'id'.

Yea right. If the primary key is different then set primary key name in the user model.
protected $primaryKey = "UserId";

So that the below function will return the value of the primarykey,

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

So the jwt token claim "sub" will contain the user id value. Which is used to fetch the user information from the token.

Also you can set more custom claims in the model using ,
public function getJWTCustomClaims()
{
return ['email' => $this->getAttributeValue('Email') ];
}

This issues caused me lot of time waste. Hope this will help some one else.

I am using Lumen 5.8 and Tymon jwt 1.0.*

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CBR09 picture CBR09  路  3Comments

gandra picture gandra  路  3Comments

loic-lopez picture loic-lopez  路  3Comments

lloy0076 picture lloy0076  路  3Comments

gamelife1314 picture gamelife1314  路  3Comments