簡體   English   中英

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

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

我有一個帶有兩個模型(客戶端和用戶)並使用 JWT 的 adonis.js 框架我想在兩個模型上都進行身份驗證,但它並沒有真正起作用

我試圖指定在每個 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
    }
}

是否可以將 JWT 與客戶端和用戶模型一起使用,並且令牌 function 可以在兩者上工作? 我必須建立自己的身份驗證令牌邏輯,這真的很忙(重新發明輪子)

目前無法使用多個身份驗證模型。

最好的辦法是在您的User model 中添加一個新字段。

官方支持解答

編輯:

來源: 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 )相同的方式實現新的 model。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM