简体   繁体   English

在 Nuxt auth-module 中设置令牌

[英]Set token in Nuxt auth-module

I'm using Nuxt auth-module in my project.我在我的项目中使用Nuxt auth-module login API returns data in this structure:登录 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"
        }
     }
} 

and in nuxtconfig i seted module config like this:在 nuxtconfig 中,我像这样设置了模块配置:

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

but token did not save in the app.但令牌没有保存在应用程序中。 any solution?任何解决方案?

so I found a solution:所以我找到了一个解决方案:

  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
        }
      }
    }
  },

The structure is data.data.user and not data.user , hence IMO you'll need to specify it with a configuration like this one结构是data.data.user而不是data.user ,因此 IMO 你需要用这样的配置来指定它

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

As explained in the documentation .文档中所述。

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

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