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

React Native集成Firebase后GoogleSignIn报InvalidUserName错误

错误原因定位

  • 核心错误1:GoogleSignin.configure 中传入了错误的accountName参数,你填入的是谷歌客户端ID,该参数作用是指定预填的登录账号,填非账号格式的ID会直接触发InvalidUserName报错。
  • 核心错误2:webClientId取值错误,你引用的Providers.google.PROVIDER_ID不存在,Providers.google是GoogleAuthProvider类,没有该属性,正确值应为Firebase控制台生成的Web类型谷歌客户端ID。
  • 其他关联错误:Firebase auth实例调用混乱,同时存在导入的全局auth实例和局部重新声明的auth变量,调用时混用auth()/auth.xxx两种写法;onAuthStateChanged回调逻辑错误,不该返回JSX组件。

修复步骤

  1. 修正谷歌登录配置,删除非必要的accountName参数,填入正确的webClientId:
GoogleSignin.configure({
  // 不需要谷歌盘权限可以删掉scopes配置
  scopes: ['https://www.googleapis.com/auth/drive.readonly'],
  // 替换为你Firebase控制台>认证>登录方式>Google中显示的Web客户端ID
  webClientId: "YOUR_GOOGLE_WEB_CLIENT_ID",
  offlineAccess: true,
  forceConsentPrompt: true,
});
  1. 清理LoginScreen中重复的auth声明,修正登录方法:
    删除两行冗余代码:
const {Firebase} = require('../config/firebase');
const auth = Firebase.auth;

修改谷歌登录逻辑:

async function onGoogleButtonPress() {
  try {
    // 先检查谷歌服务是否可用
    await GoogleSignin.hasPlayServices();
    const {idToken} = await GoogleSignin.signIn();
    const googleCredential = Providers.google.credential(idToken);
    return auth.signInWithCredential(googleCredential);
  } catch (err) {
    console.error('谷歌登录报错:', err);
    throw err;
  }
}
  1. 修正认证状态监听的逻辑,移除错误的组件返回写法,用导航跳转:
useEffect(() => {
  const unsubscribe = auth.onAuthStateChanged((user: FirebaseUser) => {
    if (user) {
      navigation.navigate('ProtectedScreen');
    }
  });
  // 组件卸载时取消监听
  return unsubscribe;
}, [navigation]);
  1. 额外检查:确认你的安卓应用的SHA1签名指纹已经正确添加到Firebase控制台的安卓应用配置中,签名不匹配也会触发登录失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 09:51:03