Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AdonisJs - Custom JWT uid field key

I have built an API using Laravel, with Laravel Passport. Everything works great, but what I'm trying to achive now, is to build a new API using AdonisJs - for a few smaller things for the website but have the authentication same for both framework's - which I guess is possible using the JWT token.

Both Laravel and Adonis are configured to use the same private / public keys for encrypting. The problem I`m facing is that after logging-in via Laravel, I cant read the user from the token, and thats due to the JWT object being different.

Adonis expects the payload to have a uid propery, which doesnt exists in Laravel passport made token.

Below is the token created by passport:

{ aud: '9',
  jti:
   'ab9d81a20ed72e263f3f6bacef91962bfcb41b5523787fa301f56fa4978e5ae9ceb22dccbfb9fcef',
  iat: 1575970326,
  nbf: 1575970326,
  exp: 1607592726,
  sub: '332',
  scopes: [],
}

And ,here is the token created by Adonis

{ uid: 332, iat: 1575971236 }

There is any way to either set uid from Laravel, or read a different field from Adonis?

Thanks!

like image 626
Ben Heimberg Avatar asked Jan 25 '26 12:01

Ben Heimberg


1 Answers

May be You Can do something like this:

while creating token add extra field in token

 $customClaims = ['token_for' => `web` or 'mobile'];
 $token = JWTAuth::claims($customClaims)->fromUser($user);

and check in any middleware or anywhere there you need

  $payload = JWTAuth::parseToken()->getPayload();
  $tokenFor = $payload->get('token_for');

now here check $tokenFor web or mobile

and go further process as per you need for web and mobile

like image 182
Ajay Avatar answered Jan 27 '26 03:01

Ajay