Firebase Authentication中Google登录与邮箱登录无法自动合并问题排查
Firebase Auth账户合并异常排查方案
问题描述
Web应用采用Firebase Authentication,支持Google登录和邮箱密码登录,2023年10月21日前运行正常。此前用户用abcd@gmail.com注册邮箱密码账户后,用同一邮箱进行Google登录时,Firebase会自动合并两个账户并保留邮箱密码凭据;但现在Google登录后,再尝试邮箱密码登录会抛出auth/wrong-password错误,疑似Google登录覆盖了原有凭据。
相关代码
注册(Sign-up)
const signUpWithEmailAndPassword = async (email, password) => { try { const authInstance = getAuth(); return await createUserWithEmailAndPassword( authInstance, email, password ); } catch (err) { console.log('err', err); if (err.code === 'auth/invalid-email') { throw new Error('Email address is not valid'); } else if (err.code === 'auth/email-already-in-use') { throw new Error('Email already existed'); } else if (err.code === 'auth/network-request-failed') { throw new Error('Please check your connection'); } else { throw new Error( 'An error occurred during sign up. Please try again later.' ); } } };
邮箱密码登录(Sign-in with email/password)
const signInWithEmailAndPassword = async (email, password) => { try { const authInstance = getAuth(); console.log('Auth instance', authInstance); return await emailPasswordSignIn(authInstance, email, password); } catch (err) { console.log(err.code); if ( err.code === 'auth/user-not-found' || err.code === 'auth/wrong-password' ) { throw new Error( 'Incorrect email or password. Please try again.' ); } else if (err.code === 'auth/network-request-failed') { throw new Error('Please check your connection'); } else if (err.code === 'auth/invalid-email') { throw new Error('Please provide valid email'); } else { throw new Error( 'An error occurred during sign in. Please try again later.' ); } } };
Google登录(Sign-in with google)
const signInWithGoogle = async () => { try { console.log('Google sign-in started'); return await signInWithPopup(auth, googleProvider); } catch (err) { console.error(err); throw err; } };
排查解决步骤
- 检查Firebase控制台用户详情:登录Firebase控制台,进入Authentication→用户列表,找到
abcd@gmail.com对应的用户,查看「登录方法」列表,确认邮箱密码凭据是否存在,以及当前账户UID是否与之前邮箱密码登录的UID一致(若出现两个独立账户,说明自动合并未触发)。 - 验证账户合并规则配置:进入Firebase控制台→Authentication→设置→高级,找到「账户合并」选项,确认是否开启了「自动合并使用同一邮箱的账户」(默认开启,但可能因平台更新被修改)。
- 排查Firebase SDK版本:检查项目中
firebase/auth的依赖版本,确认是否在10月21日后自动更新。若有版本变更,回退到之前正常的版本(如9.23.0)测试是否恢复正常。 - 调试用户凭据状态:在Google登录成功后,添加代码打印当前用户信息:
确认邮箱密码凭据是否被保留,以及用户UID是否正确。const user = getAuth().currentUser; console.log('User provider data:', user.providerData); console.log('Has password credential:', !!user.passwordHash); - 测试手动账户关联:当检测到用户已存在邮箱密码账户时,手动调用
linkWithCredential方法关联Google凭据,示例代码:// 假设已获取Google登录的credential const credential = GoogleAuthProvider.credentialFromResult(result); const user = getAuth().currentUser; await user.linkWithCredential(credential); - 查看Auth操作日志:进入Firebase控制台→Authentication→日志,搜索
abcd@gmail.com的操作记录,查看是否有账户凭据修改、移除的日志条目,定位问题触发点。
内容的提问来源于stack exchange,提问作者Madhav mishra
相关产品推荐
相关产品推荐

