在使用google firebase auth登录之前如何查找用户是否有以前的登录凭据



我正在创建我的登录页面在我的webapp上,我已经成功地实现了谷歌注册按钮。对于新用户来说,它工作得很好,但是当现有用户应该登录时,它应该抛出"auth/account-exists-with-different-credential"的错误,而不是抛出,因此以前的用户数据的电子邮件和密码被完全覆盖,而不是被合并在一起。这是我的代码片段,任何帮助将不胜感激

const googleProvider = new firebase.auth.GoogleAuthProvider();
export const signinGoogle = () => {
auth
.signInWithPopup(googleProvider)
.then((res) => {
console.log(res.user);
})
.catch(function (error) {
console.log(error.message);
if (error.code === 'auth/account-exists-with-different-credential') {
var pendingCred = error.credential;
var email = error.email;
auth.fetchProvidersForEmail(email).then(function (methods) {
if (methods[0] === 'password') {
var password = prompt('Enter your password');
auth.signInWithEmailAndPassword(email, password).then(function (result) {
return result.user.linkWithCredential(pendingCred);
});
}
});
}
});
};

我已经按照https://firebase.google.com/docs/auth/web/google-signin#expandable-1做了一切,但主要问题是错误没有抛出

从你的链接文档说:

当用户登录Google时,如果他们的帐户托管在Google上,即使他们使用密码或社交IDP注册帐户,也不会导致此错误。

这意味着如果某人注册了电子邮件和密码,然后登录谷歌,它将覆盖之前的数据。密码丢失

您可以使用firebaseAuth.fetchSignInMethodsForEmail来检查电子邮件使用的提供商并重定向到该提供商,但对于那些已经使用Google sign的人来说,它不会成为一个很好的UX。因为他们需要手动输入电子邮件才能进入正确的登录方法。

如果用户在同一设备上,你可以将最后的登录方法存储在本地存储的某个地方,并使用它将用户路由到正确的方向。

不幸的是,对于这种行为没有严格而好的解决方案。

最新更新