Jwt-auth: customClaims not added into token

Created on 28 Oct 2017  路  7Comments  路  Source: tymondesigns/jwt-auth

I use Laravel 5.5 and "tymon/jwt-auth": "dev-develop" version
I try to add custom claims using this code

$customClaims = [ 'name' => $user->name, 'username' => $user->username, 'role'=> $user->userRole->role->name, 'timestamp'=>'test', ];

$token = \JWTAuth::attempt($credentials, $customClaims)

But when i try to encode the token it just have default payload like this

{ "iss": "http://localhost:8000/api/login", "iat": 1509195911, "exp": 1509199511, "nbf": 1509195911, "jti": "xjVxZ5GebMWtEOcH", "sub": 4, "prv": "87e0af1ef9fd15812fdec97153a14e0b047546aa" }

with no custom claim added.

Any help???

Most helpful comment

@cinilak onapp/User.php you set de attributes that will appear on the Claim of the token.
Like this:

  public function getJWTCustomClaims()
    {
         return ["name" => $this->name ];
    }

When you decode the token, that attribute will appear on the claims data.

All 7 comments

try adding the claims through User model

@akkhan20 Hi, how can we do that? i have de dev version and i need to set the claims but on the JWTAuth.php doesn't have for more parameters.

@faruqisan Did you resolve this issue and if so, how? Thanks

@cinilak on the model User, on the properties just add the attributes that you want

@ulisescarreonalvarez Could you give me an example? I'm not sure what you mean. Thanks!

@cinilak onapp/User.php you set de attributes that will appear on the Claim of the token.
Like this:

  public function getJWTCustomClaims()
    {
         return ["name" => $this->name ];
    }

When you decode the token, that attribute will appear on the claims data.

I'm having the same issue, I need different types of tokens, with custom exp dates

Was this page helpful?
0 / 5 - 0 ratings

Related issues

therealmjk picture therealmjk  路  3Comments

kofi1995 picture kofi1995  路  3Comments

johncloud200 picture johncloud200  路  3Comments

lloy0076 picture lloy0076  路  3Comments

CBR09 picture CBR09  路  3Comments