首页 > 解决方案 > Nuxt Auth 两个本地端点

问题描述

我想制作两个本地端点,如下所示:

strategies: {
  localOne: {
    endpoints: {
      login: { url: "token/", method: "post", propertyName: "access" },
      user: { url: "user/me/", method: "get", propertyName: false },
      logout: false,
    }
  },
  localTwo: {
    endpoints: {
      login: { url: "user/token/", method: "post", propertyName: "access" },
      user: { url: "user/me/", method: "get", propertyName: false },
      logout: false,
    }
  }
},

但我在控制台中遇到以下问题

client.js?06a0:77 TypeError: Cannot read property 'mounted' of undefined
at Auth.mounted (auth.js?facc:112)
at Auth.setStrategy (auth.js?facc:108)
at Auth.loginWith (auth.js?facc:123)
at _callee3$ (log-in.vue?f35c:175)
at tryCatch (runtime.js?96cf:45)
at Generator.invoke [as _invoke] (runtime.js?96cf:271)
at Generator.prototype.<computed> [as next] (runtime.js?96cf:97)
at asyncGeneratorStep (asyncToGenerator.js?1da1:3)
at _next (asyncToGenerator.js?1da1:25)
at eval (asyncToGenerator.js?1da1:32)

如何在 nuxt js 中为两个不同的身份验证创建两个端点?先感谢您

标签: vue.jsnuxt.js

解决方案


我想出了不同的决定来让它发挥作用。终于成功了,

 this.$axios.post('user/token/', {
              id : res.data.id,
              firstname: res.data.firstname,
              lastname : res.data.lastname,
              email: res.data.email
          })
          .then((resp) => {
            this.$auth.setToken('local', 'Bearer ' + resp.data.access)
            this.$axios.setHeader('Authorization', 'Bearer ' + resp.data.access)
            this.$auth.ctx.app.$axios.setHeader('Authorization', 'Bearer ' + resp.data.access)
          })
          .then(() => {
            this.$axios.get('user/me/')
            .then((resp) => { 
              this.$auth.setUser(resp.data); 
              this.$router.push('/') 
            })
          .catch(() => {
          console.log(err)
          })

让我用简单的话解释一下。我没有使用 nuxt auth local,而是使用它的方法来达到与本地身份验证登录相同的结果。首先,我将数据发布到服务器以创建用户,然后使用其数据登录。简单明了。如果有人需要,我可以更深入地解释它。


推荐阅读