【问题标题】:NuxtJS Auth ProxyNuxtJS 身份验证代理
【发布时间】:2020-12-07 13:58:24
【问题描述】:

然后使用nuxtjs/authnuxtjs/axios nuxt 忽略我的代理配置。

过去我只使用 axios 进行身份验证。

现在我正在尝试使用@nuxtjs/auth 模块。因为我使用单独的后端和 CORS,所以我需要使用 axios 代理进行身份验证。

但身份验证策略local 不使用代理,我不明白为什么。它总是尝试使用 nuxt URL。好像 auth 完全忽略了我的代理。有人可以帮忙吗?

// nuxt.config
// ...
axios: {
    proxy: true
  },
  proxy: {
    '/api/': {
      target: 'http://localhost:4000',
      pathRewrite: { '^/api/': '' }
    }
  },
  /*
  ** Auth Module Configuration
  ** See https://auth.nuxtjs.org/schemes/local.html
  */
  auth: {
    strategies: {
      local: {
        endpoints: {
          login: { url: '/api/auth/login', method: 'post', propertyName: 'token' },
          logout: { url: '/api/auth/logout', method: 'post' },
          user: { url: '/api/auth/user', method: 'get', propertyName: 'user' }
        }
      }
    }
  },
// ..
// Component
async userLogin() {
      try {
        let response = await this.$auth.loginWith('local', { data: this.login })
        console.log(response)
      } catch (err) {
        console.log(err)
      }
    }

我的 Nuxt 在 http://localhost:3000 上运行

我的客户端总是尝试连接到 http://localhost:3000/api/auth/login

但我需要 http://localhost:4000/auth/login

我使用最新的所有模块

【问题讨论】:

    标签: authentication proxy axios nuxt.js nuxtjs


    【解决方案1】:

    proxy 对象应该在axios 对象之外,即

    axios: {
     proxy: true,
     // Your other configurations
    }
    
    proxy: {
     '/api/': {
       target: 'http://localhost:4000',
       pathRewrite: { '^/api/': '' }
     }
    }
    

    【讨论】:

    • 在提供的问题中,@equaliser 已经将代理放在 axios 对象之外,唯一使您的答案与他的问题不同的是,他在对象之后放置了逗号,而您没有(这导致解析失败)
    猜你喜欢
    • 2019-12-07
    • 2020-08-15
    • 1970-01-01
    • 2020-05-28
    • 2023-03-25
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多