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

AWS Amplify v6使用signInWithRedirect无法跳转Google账号选择页问题

AWS Amplify v6 signInWithRedirect 不跳转Google登录页面的排查与解决

核心问题

迁移到Amplify v6后,调用signInWithRedirect({ provider: 'Google' })无法跳转至Google账号选择页,需从配置、平台设置、代码细节三方面排查修复。


排查与修复步骤

1. 匹配Cognito身份提供者ID

Amplify v6中provider参数必须和Cognito用户池内配置的Google身份提供者ID完全一致(区分大小写):

  • 登录AWS控制台,进入目标Cognito用户池 → 身份提供者 → 查看Google提供者的「提供者ID」;
  • 确保代码中signInWithRedirect的provider值与该ID完全匹配(默认是Google,若自定义过名称则需对应修改)。

2. 完善OAuth配置项

v6需要显式声明支持的OAuth提供者,补充配置中的providers字段:

Amplify.configure({
    Auth: {
        Cognito: {
            userPoolId: 'us-west-xxxxx',
            userPoolClientId: 'xxxxxxx',
            loginWith: {
                oauth: {
                    domain: 'auth.abcdefgh.com',
                    redirectSignIn: ["http://localhost:3000/"],
                    redirectSignOut: ["http://localhost:3000/"],
                    responseType: 'code',
                    scope: ['email', 'openid'],
                    providers: ['Google'] // 新增:显式指定支持的OAuth提供者
                }
            }
        }
    }
});

3. 校验跨平台URL配置

  • Cognito端:确保用户池客户端的「回调URL」「注销URL」与redirectSignIn/redirectSignOut完全一致(包含协议、端口,localhost不能省略端口);
  • Google OAuth应用端:在Google开发者控制台的OAuth应用设置中,添加Cognito的回调地址:https://auth.abcdefgh.com/oauth2/idpresponse,这是Cognito处理第三方登录回调的固定地址,必须授权。

4. 捕获隐藏错误

为代码添加错误捕获,排查是否存在配置或权限类隐性错误:

const handleSocialSignIn = async () => {
  try {
    await signInWithRedirect({
      provider: 'Google'
    });
  } catch (err) {
    console.error('Google登录跳转失败:', err);
    // 根据错误信息定位问题,如provider不存在、权限不足等
  }
}

5. 清除缓存测试

旧的登录会话缓存可能导致跳转异常,清除浏览器中Cognito相关Cookie和缓存后重新测试。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 07:31:02