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

Expo React Native Android应用中Google登录出现“页面未找到”且Firebase认证状态无法持久化的问题

Expo React Native Android应用中Google登录出现“页面未找到”且Firebase认证状态无法持久化的问题

兄弟,我之前开发Expo React Native Android应用时,正好踩过和你一模一样的坑——Google登录跳404,好不容易登进去又自动跳回登录页,Auth状态完全留不住。给你分享几个亲测有效的解决办法:

  • 先搞定“页面未找到”的回调问题
    这个90%是因为OAuth回调URI没配置对。你得去Firebase控制台的「身份验证」→「登录方法」→「Google」里,把生产环境的回调URI加上:com.anonymous.lifelog:/oauthredirect/google(就是你的包名加:/oauthredirect/google)。我之前就是漏加了这个,导致Google认证完找不到跳转回应用的入口,直接跳404了。

  • 检查Expo配置里的Scheme和Intent过滤
    打开你的app.json,先确认android.package确实是com.anonymous.lifelog,然后加上对应的Intent过滤,确保应用能捕获Google的回调请求:

    "android": {
      "package": "com.anonymous.lifelog",
      "googleServicesFile": "./google-services.json",
      "intentFilters": [
        {
          "action": "VIEW",
          "data": [
            {
              "scheme": "com.anonymous.lifelog",
              "host": "oauthredirect/google"
            }
          ],
          "category": [
            "BROWSABLE",
            "DEFAULT"
          ]
        }
      ]
    }
    

    另外用expo-auth-session的makeRedirectUri时,一定要指定native的scheme,别用默认的web地址:

    import { makeRedirectUri } from 'expo-auth-session';
    
    const redirectUri = makeRedirectUri({
      native: 'com.anonymous.lifelog:/oauthredirect/google',
    });
    
  • 强制Firebase Auth持久化状态
    有时候生产环境下Firebase Auth的默认持久化会出问题,你得显式设置成本地持久化,在初始化Auth后就加上这段代码:

    import { getAuth, setPersistence, browserLocalPersistence } from "firebase/auth";
    
    const auth = getAuth();
    setPersistence(auth, browserLocalPersistence)
      .then(() => {
        // 这里再执行你的Google登录逻辑
      })
      .catch((error) => {
        console.error('持久化设置失败', error);
      });
    

    注意别用native的持久化选项,Expo环境下browserLocalPersistence才是适配webview登录流程的。

  • 确保登录后正确同步到Firebase Auth
    用expo-auth-session拿到Google的idToken后,必须把凭证传给Firebase Auth,不然Firebase根本不会记录用户状态:

    import { GoogleAuthProvider, signInWithCredential } from "firebase/auth";
    
    // 拿到expo-auth-session返回的idToken后
    const credential = GoogleAuthProvider.credential(idToken);
    signInWithCredential(auth, credential)
      .then((userCredential) => {
        // 这里再导航到主页,别直接跳,等Firebase确认状态后再操作
      })
      .catch((error) => {
        console.error('登录凭证同步失败', error);
      });
    
  • 最后注意生产构建的方式
    别用Expo Go预览生产环境的登录流程,一定要用eas build -p android打正式包测试,Expo Go的Scheme和你自己的生产包不一样,会导致回调和Auth状态都出问题。

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.08 15:03:01