【问题标题】:How can i use multiple model jwt auth on adonis.js我如何在 adonis.js 上使用多个模型 jwt auth
【发布时间】:2019-10-16 23:42:05
【问题描述】:

我有一个带有两个模型(客户端和用户)的 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 与客户端和用户模型一起使用,并且令牌功能在两者上都可以使用?我必须建立自己的身份验证令牌逻辑,这真的很忙(重新发明轮子)

【问题讨论】:

  • 你的 laravel 应用中的客户端模型是否实现了可认证?
  • @Saly3301 它的 adonis.js(我实际上使用 laravel 处理多个身份验证)我只是想要一个多线程后端,这就是我选择 adonis(非常类似于 laravel)的原因

标签: node.js authentication jwt adonis.js


【解决方案1】:

目前无法使用多种身份验证模型。

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

Official support answer

编辑:

来源:forums.adonisjs.com

可以将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)。

【讨论】:

  • 好的,我想我必须自己构建它,也许可以用它制作一个包
  • 是的,或者您可以创建具有用户角色的自定义身份验证中间件,例如:gitlab.com/CrBast/website/blob/develop/website/app/Middleware/…(这不是您想要的,但这是一个想法)
  • 当然。会将我的方法更改为类似于您建议的方法。谢谢
  • 所以我改变了我的方法,仍然设法使用两个身份验证系统。在 Adonis.js 完全支持默认用户身份验证的情况下,我为 Admin 编写了自己的身份验证逻辑,尽管我必须使用自己的方法来管理用户登录状态
  • 是的,它有帮助,令人惊讶的是我后来发现了这种方法,但我遇到的挫折是我无法检查客户是否使用令牌登录,我相信我对此缺乏一些了解尽管。谢谢
猜你喜欢
  • 2016-08-18
  • 2018-06-26
  • 2019-09-29
  • 1970-01-01
  • 1970-01-01
  • 2022-10-21
  • 2021-08-06
  • 2019-09-22
  • 2017-01-14
相关资源
最近更新 更多