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组件。
修复步骤
- 修正谷歌登录配置,删除非必要的
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, });
- 清理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; } }
- 修正认证状态监听的逻辑,移除错误的组件返回写法,用导航跳转:
useEffect(() => { const unsubscribe = auth.onAuthStateChanged((user: FirebaseUser) => { if (user) { navigation.navigate('ProtectedScreen'); } }); // 组件卸载时取消监听 return unsubscribe; }, [navigation]);
- 额外检查:确认你的安卓应用的SHA1签名指纹已经正确添加到Firebase控制台的安卓应用配置中,签名不匹配也会触发登录失败。
内容的提问来源于stack exchange,提问作者Manuel Lucas
相关产品推荐
相关产品推荐

