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???
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
Most helpful comment
@cinilak on
app/User.phpyou set de attributes that will appear on the Claim of the token.Like this:
When you decode the token, that attribute will appear on the claims data.