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

Expo + React Native 集成Google登录到Firebase时,完成账户确认后出现异常页面的问题求助

Expo + React Native 集成Google登录到Firebase时,完成账户确认后出现异常页面的问题求助

大家好,我目前在用Expo开发React Native应用,想把Google登录集成到Firebase里,但遇到了个卡壳的问题,折腾了好久都没解决,来请教下各位!

先说说我的配置情况

在Google Cloud Console里我是这么设置OAuth 2.0客户端的:

  • 应用类型:Web应用
  • 已授权的JavaScript来源:https://auth.expo.io
  • 已授权的重定向URI:https://auth.expo.io/@我的用户名/我的应用slug(这里的用户名和slug都是我实际项目的对应信息)

我的代码实现

下面是我相关的登录逻辑代码:

1. Firebase登录处理函数

export const signInWithGoogle = async (idToken: string) => {
  try {
    const credential = GoogleAuthProvider.credential(idToken);
    const userCredential = await signInWithCredential(firebaseAuth, credential);
    const user = userCredential.user;
    return { user };
  } catch (error) {
    throw error;
  }
};

2. Google登录请求配置与响应监听

const redirectUri = "https://auth.expo.io/@我的用户名/我的应用slug";
const [request, response, promptAsync] = Google.useIdTokenAuthRequest({
  clientId: "<我的客户端ID>",
  redirectUri,
});

useEffect(() => {
  const handleGoogleSignIn = async () => {
    if (response?.type === "success") {
      const { id_token } = response.params;
      try {
        const result = await signInWithGoogle(id_token);
        console.log("Firebase sign-in success:", result);
      } catch (error: any) {
        console.error("Firebase sign-in error:", error);
        Alert.alert("Erro no login", error.message || "Erro desconhecido");
      }
    }
  };

  handleGoogleSignIn();
}, [response]);

3. 触发登录的按钮逻辑

async () => {
  if (request) {
    await promptAsync();
  }
}

问题现象

我用Expo Go测试应用时,登录流程是这样的:
选择账户 → 确认登录 → 显示“登录完成”
但到“登录完成”这一步,我会跳转到一个异常页面,没法正常回到应用里完成登录。我反复检查了Client ID、重定向URI这些配置项,看起来都是对的,但就是卡在最后这一步,实在找不到问题出在哪了。

有没有大佬遇到过类似的情况,或者能帮我看看代码哪里有疏漏的地方吗?万分感谢!

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.07 11:09:29