Nuxt Auth两个本地端点

问题描述 投票:4回答:1

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

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.js nuxt.js
1个回答
0
投票

我想出了不同的决定来使它起作用。终于成功了,

 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,而是使用其方法来获得与本地auth登录相同的结果。首先,我将数据发布到服务器以创建用户,然后使用其数据登录。简单明了。如果有人需要,我可以更深入地解释。

© www.soinside.com 2019 - 2024. All rights reserved.