简体   繁体   English

我如何在 adonis.js 上使用多个 model jwt auth

[英]How can i use multiple model jwt auth on adonis.js

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我有一个带有两个模型(客户端和用户)并使用 JWT 的 adonis.js 框架我想在两个模型上都进行身份验证,但它并没有真正起作用

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我试图指定在每个 model 上使用什么身份验证,以及如何构建令牌、用户和客户端数据库来支持这个

// 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?是否可以将 JWT 与客户端和用户模型一起使用,并且令牌 function 可以在两者上工作? I have to build my own auth token logic which is really hectic (re-inventing the wheel)我必须建立自己的身份验证令牌逻辑,这真的很忙(重新发明轮子)

Currently it's not possible to use multiple authentication models.目前无法使用多个身份验证模型。

The best is to add a new field in your User model.最好的办法是在您的User model 中添加一个新字段。

Official support answer 官方支持解答

Edit:编辑:

Source: forums.adonisjs.com来源: forums.adonisjs.com

It would be possible to configure auth.js to use multiple models:可以将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`,
    },
  },

switch authentication:切换认证:

await auth.authenticator('anotherAuth')

Be careful to implement the new model in the same way as the default ( User ).小心以与默认( User )相同的方式实现新的 model。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM