我有一个包含两个模型(客户端和用户)的adonis.js框架,并且使用JWT,我希望对这两个模型都有自己的看法,但它并不真正有效。
我试图指定在每个模型上使用什么身份验证,以及如何构造令牌、用户和客户端数据库以支持这一点。
// 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
}
}是否可以在客户端和用户模型中使用JWT,并让令牌函数在这两种模型上都使用?我必须建立我自己的象征逻辑,这是非常繁忙的(重新发明车轮)。
发布于 2019-10-17 06:01:46
目前不可能使用多个身份验证模型。
最好是在User模型中添加一个新字段。
编辑:
可以将auth.js配置为使用多个模型:
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`,
},
},交换机身份验证:
await auth.authenticator('anotherAuth')请注意,以与默认(User).相同的方式实现新模型
https://stackoverflow.com/questions/58423004
复制相似问题