Is there a way to use JWT with uuid ? My users table has a "id" field but this field isn't int but uuid (char 36).
When I try to get the user by token (JWTAuth::toUser) using uuid (char 36) as my id field type, I get "user_not_found" error; but when I use int as my id field type I get the user correctly.
Is there a way to fix it ?
jwt.php file has config attribute for that.
'identifier' => 'id',
and can be changed to your custom storage field.
Yes, but in this case my identifier still is "id". I just changed the type of"id". This isn't a int anymore but char.
I tried to change:
'identifier' => 'uuid',
but still not working.
Any way to use "id" with other type (like char) and not int ?
Looks like then you need to write your own custom authentication or user adapter.
In config there are lines:
'user' => Tymon\JWTAuth\Providers\User\EloquentUserAdapter::class,
'auth' => Tymon\JWTAuth\Providers\Auth\IlluminateAuthAdapter::class,
which can be swapped with your own auth or user implementations. I did that to use JWT with Sentinel package functionality, but I can't find that code right now. There is another, older implementation example that uses functionality of Sentry auth package:
https://gist.github.com/hackel/00a2b5a43d5007eceaa1
Maybe that would help you to write your custom implementation.
Thank you! I will try then
is the uuid the primary key on your user model?
Yes!
Perfect, so here is what i've done and it works perfects.
in App\User I made sure to set my primary key as follows
/**
* [$primaryKey description]
* @var string
*/
protected $primaryKey = 'db_column_name';
in jwt.php config
'identifier' => 'db_column_name',
Now when you get your JWT token using the example in this repo and then plug it into jwt.io with your hash and you should see that your "sub" is the uuid since JWT will use the sub field to find the user by primary key.
Let me know if it works :)
@shalomabitan Yes! I tried this but still isn't working. It worked for you ?
In my case seems that the problem occurs when the length of my "id" is bigger then 篓9".
When I try to use uuid it doesn't works, but when my id is int with length minor or equals 9 (like 11111111) then works fine, but when that length is bigger (like 1111111111) JWT doesn't get the right user with _JWTAuth::toUser_ method. I think that it is the reason why uuid is not working properly.
If you like, I can create a project to show that, what do you think about it ?
okay, so I set up my user model as follows. Perhaps you're missing something. Also, what version of laravel you using?
```
namespace App;
use Illuminate\Auth\Authenticatable;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The connection name for the model.
*
* @var string
*/
protected $connection = 'pgsql';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
/**
* [$primaryKey description]
* @var string
*/
protected $primaryKey = 'uuid';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token' ];
```
@shalomabitan I'm using 5.2 version. Ok! I'll try again tonight, and I will post the result! Thanks
Thanks @shalomabitan and @ddctd143 it worked now!
The problem was the keytype. I was using the default (int), and now I just changed the property $keytype to char and worked!
protected $keyType = 'char';
here is the example:
https://github.com/Arkanius/jwt-laravel-test
Thanks!
Hi,
I'm having the same issue with Laravel 5.4, none of solutions above worked.
My User table has UUID as primary key, the column name is 'id' and this is my User Model:
class User extends Authenticatable
{
public $incrementing = false;
protected $connection = 'pgsql';
protected $primaryKey = 'id';
protected $keyType = 'string';
...
Do you guys have any suggestion?
@andrealune Try to change the $keyType = 'string'; to $keyType = 'char';
All you need to do for UUID keys is this in your "base" model
/**
* @var bool Set to false for UUID keys
*/
public $incrementing = false;
/**
* @var string Set to string for UUID keys
*/
protected $keyType = 'string';
And in your User model, there shouldn't be any reason to do anything fancy - ie;
/**
* For Authentication
* Get the identifier that will be stored in the subject claim of the JWT.
*
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey();
}
/**
* Get the name of the unique identifier for the user.
*
* @return string
*/
public function getAuthIdentifierName()
{
return $this->getKeyName();
}
If it still doesn't work, maybe try setting the actual $primaryKey on your Model
@specialtactics specialtactics
I'm using laravel 6.2 and I did what you mentioned, but it didn't work for me
What specifically does not work @saulo1212 ? I suspect you must have some other customisations on top, I have been using that type of setup since version 5, and it's never broken.
Most helpful comment
Perfect, so here is what i've done and it works perfects.
in
App\UserI made sure to set my primary key as followsNow when you get your JWT token using the example in this repo and then plug it into jwt.io with your hash and you should see that your "sub" is the uuid since JWT will use the
subfield to find the user by primary key.Let me know if it works :)