3

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!

Ohgodwhy
  • 44,803
  • 8
  • 64
  • 95

2 Answers2

3

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

Ajay
  • 644
  • 6
  • 14
  • 1
    I searched, but have not found a way to add custom claims to token by passport. Do you have any reference for that maybe? Thanks! – Ben Heimberg Dec 10 '19 at 11:26
0

If I'm reading your question correctly, you just want to change the model key used for the JWT uid field which by default is the id of the record in the DB. This is because the Adonis JWT implementation by default uses the model primary key.

To override this, in your User model (or whatever model you have set in your config/auth.js jwt.model model) set

static get primaryKey() {
  return 'uuid' // Or whatever you want to be the uid
}

Note: this may cause side effects with relationships and query performance if the primary key for the model is a UUID