Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to add custom claim in Tymon JWT

Tags:

php

laravel

jwt

I'm using Tymon JWT to generate my token in Laravel. I have followed the guide in Tymon's github site carefully to add my custom claims like so:

$customClaims = ['foo' => 'bar', 'baz' => 'bob'];
JWTAuth::attempt($credentials, $customClaims);

I managed to generate a token after authenticating the user, but when I decode the token with JWT decoder, I only see the default claims, but not my custom claim.

like image 361
schenker Avatar asked Jan 10 '17 15:01

schenker


2 Answers

You are using Tymon JWT version 1.0.0 maybe?

From Github Tymon JWT

For version 0.5.* See the WIKI for documentation.

Documentation for 1.0.0 is coming soon, but there is an unfinished guide here

Use

JWTAuth::customClaims(['foo' => 'bar'])->attempt(['login' => '', 'password' => '']);
like image 69
maxkrasnov Avatar answered Oct 11 '22 16:10

maxkrasnov


You can using:

$store = [
    'id'    => 1,
    'name'  => 'Store1'
];

$token = JWTAuth::customClaims(['store' => $store])->fromUser($user);

And get info:

$storeData = JWTAuth::getPayload()->get('store');
like image 37
binhhoang18 Avatar answered Oct 11 '22 15:10

binhhoang18