7

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

过去我只使用 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

我使用最新的所有模块

4

1 回答 1

-2

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

axios: {
 proxy: true,
 // Your other configurations
}

proxy: {
 '/api/': {
   target: 'http://localhost:4000',
   pathRewrite: { '^/api/': '' }
 }
}
于 2021-10-18T11:58:53.837 回答