Multi model in jwt authentication

#1

Hey, is it possible to have more than one model in jwt authentication?

Currently i have this auth config:

{
  user: {
    serializer: 'lucid',
    model: 'App/Models/User',
    scheme: 'jwt',
    uid: 'cpf',
    password: 'password',
    options: {
      secret: Env.get('APP_KEY')
    }
  },
  admin: {
    serializer: 'lucid',
    model: 'App/Models/Admin',
    scheme: 'jwt',
    uid: 'cpf',
    password: 'password',
    options: {
      secret: Env.get('APP_KEY')
    }
  }
}

And i wanted that both users could use the same route using auth middleware, i tried:

.middleware('auth:user,auth:admin')
.middleware(['auth:user','auth:admin'])

But in controller auth always returns from the user table, even when using token from admin_tokens.

Is it possible or do i have to use different routes?

Thanks

Edit: I found out that secret can’t be the same, make them different and it works.

0 Likes

#2

Edit: I found out that secret can’t be the same, make them different and it works.

Could you explain better? I have the same problem here. xD I need that tokens are generated in another table.

0 Likes

#3

Like this:

{
  user: {
    serializer: 'lucid',
    model: 'App/Models/User',
    scheme: 'jwt',
    uid: 'cpf',
    password: 'password',
    options: {
      secret: Env.get('APP_KEY') + 'user' // <-
    }
  },
  admin: {
    serializer: 'lucid',
    model: 'App/Models/Admin',
    scheme: 'jwt',
    uid: 'cpf',
    password: 'password',
    options: {
      secret: Env.get('APP_KEY') + 'admin' // <
    }
  }
}
0 Likes