You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Nuxt Auth Google社交登录请求适配dj-rest-auth后端要求

Nuxt Auth对接Django dj-rest-auth社交登录请求格式不兼容问题及解决

我使用dj-rest-auth / django allauth搭建了社交认证功能,后端接口要求接收JSON格式的相关认证信息,后端期望的请求格式如下:
后端期望请求示例

但我发现Nuxt Auth虽然会发起POST请求,却默认后端应当从URL中提取认证信息,实际运行流程为:点击Google登录按钮→Nuxt Auth跳转至Google授权页→Google授权完成后跳转至我配置的回调URL,所有认证信息都携带在URL参数中→Nuxt Auth直接将完整URL原封不动作为请求内容发送。
Nuxt Auth实际发送请求示例

目前我有两个可选方案:

  • 尝试修改后端适配请求格式,改动成本较高,较为麻烦
  • 设法覆盖Nuxt Auth的默认逻辑,但我暂不了解具体实现方式

官方文档提到可以创建自定义Scheme,但我始终找不到自定义实际发送的POST请求的方法,希望能得到相关指点。

我的nuxt.config.js配置如下:

auth: {
    redirect: {
      login: '/register',
      logout: false,
      // callback: '/login',
      home: '/bevakning'
    },
    strategies: {
      google: {
        scope: ['profile', 'email'],
        codeChallengeMethod: '',
        responseType: 'code',
        endpoints: {
          token: 'http://localhost:8000/social-login/google/',
          userInfo: 'http://localhost:8000/auth/user/'
        },
        // withCredentials: true,
      },
      local: {
        token: {
          property: 'key',
          type: 'Token ',
          maxAge: 86400 * 120
        },
        user: {
          // using property: false means it will use the entire json response as user and not just one variable.
          property: false
          // property: 'email'
        },
        endpoints: {
          login: {
            url: '/dj-rest-auth/login/ ',
            method: 'post',
            // propertyName: 'auth_token'
          },
          logout: {
            url: '/dj-rest-auth/logout/',
            method: 'post'
          },
          user: {
            url: '/dj-rest-auth/user/',
            method: 'get',
          }
        },
        // tokenRequired: true,
        // tokenType: 'Token ',
        // globalToken: true,
        autoFetchUser: true,
      }
    }
  },

问题更新(已解决)

我已解决该问题,根源为Axios覆盖了Nuxt Auth的默认请求方法,异常表现为请求头Content-Type已经设置为JSON格式,但请求体实际仍以x-www-urlencoded格式发送。

内容的提问来源于stack exchange,提问作者Thorvald

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 23:54:05