Reputation: 195
I have an adonis.js framework with two models (client and user) and using JWT I want to have auth on both models and it doesn't really work
I have tried to specify what authentication to use on each model and how to structure the tokens, users and clients database to support this
// I tried to set the auth.js to support them
client: {
serializer: 'lucid',
model: 'App/Models/Client',
scheme: 'jwt',
uid: 'email',
password: 'password',
options: {
secret: Env.get('APP_KEY')+'client'
}
},
//used default jwt for user model
//somewhere inside my auth function below
async clientAuth({request, auth, response}){
if(await auth.authenticator('client').attempt(email, password)){
//did some stuffs as the above worked for Client Model
let client = await Client.findBy('email', email)
// even if the auth passed, i can't store the token or generate
//the line below generate error
auth.generate(client) //says auth.generate is not a function
}
}
is it possible to use JWT with both the client and user models and have the token function working on both? I have to build my own auth token logic which is really hectic (re-inventing the wheel)
Upvotes: 0
Views: 2349
Reputation: 2302
Currently it's not possible to use multiple authentication models.
The best is to add a new field in your User
model.
Source: forums.adonisjs.com
It would be possible to configure auth.js
to use multiple models:
jwt: {
serializer: 'lucid',
model: 'App/Models/UserForum1',
scheme: 'jwt',
uid: 'email',
password: 'password',
options: {
secret: `${Env.get('APP_KEY')}-forum1`,
},
},
anotherAuth: {
serializer: 'lucid',
model: 'App/Models/UserForum2',
scheme: 'jwt',
uid: 'email',
password: 'password',
options: {
secret: `${Env.get('APP_KEY')}-forum2`,
},
},
switch authentication :
await auth.authenticator('anotherAuth')
Be careful to implement the new model in the same way as the default (User
).
Upvotes: 3