簡體   English   中英

在 Nuxt auth-module 中設置令牌

[英]Set token in Nuxt auth-module

我在我的項目中使用Nuxt auth-module 登錄 API 以這種結構返回數據:

data:{
    data:{
        user:{
        bio: null
        createdAt: "2021-06-29T12:28:42.442Z"
        email: "name@info.co"
        id: 1
        image: null
        token: "token"
        updatedAt: "2021-06-29T12:28:42.447Z"
        username: "name"
        }
     }
} 

在 nuxtconfig 中,我像這樣設置了模塊配置:

  auth: {
    strategies: {
      local: {
        endpoints: {
          login: { url: 'users/login', method: 'post', propertyName: 'data.user.token' },
          user: { url: 'me', method: 'get', propertyName: 'data' },
          logout: false
        }
      }
    }
  },

但令牌沒有保存在應用程序中。 任何解決方案?

所以我找到了一個解決方案:

  auth: {
    strategies: {
      local: {
        token: {
          property: 'user.token', // /user endpoint API returns user object
          type: 'Token' // if your token type is not Bearer
        },
        user: {
          user: 'user'
        },
        endpoints: {
          login: { url: '/users/login', method: 'post' },
          user: { url: '/user', method: 'get' },
          logout: false
        }
      }
    }
  },

結構是data.data.user而不是data.user ,因此 IMO 你需要用這樣的配置來指定它

auth: {
  strategies: {
    local: {
      user: {
        property: 'data.user', // or maybe 'data.data.user'
        // autoFetch: true
      },
    }
  }
}

文檔中所述。

暫無
暫無

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

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