首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在adonis.js上使用多模型jwt?

如何在adonis.js上使用多模型jwt?
EN

Stack Overflow用户
提问于 2019-10-16 23:42:05
回答 1查看 2.1K关注 0票数 0

我有一个包含两个模型(客户端和用户)的adonis.js框架,并且使用JWT,我希望对这两个模型都有自己的看法,但它并不真正有效。

我试图指定在每个模型上使用什么身份验证,以及如何构造令牌、用户和客户端数据库以支持这一点。

代码语言:javascript
复制
// 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,并让令牌函数在这两种模型上都使用?我必须建立我自己的象征逻辑,这是非常繁忙的(重新发明车轮)。

EN

回答 1

Stack Overflow用户

发布于 2019-10-17 06:01:46

目前不可能使用多个身份验证模型。

最好是在User模型中添加一个新字段。

官方支持答案

编辑:

来源:https://forum.adonisjs.com/t/how-to-use-multiple-table-for-multiple-authentication-in-adonisjs/1628/3

可以将auth.js配置为使用多个模型:

代码语言:javascript
复制
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`,
    },
  },

交换机身份验证:

代码语言:javascript
复制
await auth.authenticator('anotherAuth')

请注意,以与默认(User).相同的方式实现新模型

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58423004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档