我目前在 Firebase 身份验证中启用了“每个电子邮件地址一个帐户”设置。问题是,当用户使用已用于创建电子邮件/密码帐户的相同电子邮件地址通过 Google OAuth 登录时,Google OAuth 会自动将电子邮件/密码提供商替换为自己的电子邮件/密码提供商。
我在错误处理过程中进行了以下设置,以便提示用户选择是否链接两个帐户:
if (error.code === 'auth/email-already-in-use' || error.code === 'auth/credential-already-in-use' || error.code === 'auth/account-exists-with-different-credential')
但 Google OAuth 甚至不会产生任何让我捕捉到的错误。它只是默默地替换现有帐户。
最佳答案
默认情况下,Firebase 允许每个用户一封电子邮件。如果已使用电子邮件和密码注册的用户稍后使用 Google OAuth 登录。这些用户详细信息将更新为 Google OAuth 用户详细信息。此外,电子邮件会自动验证。大多数应用程序都是这样工作的。 您可以尝试这样做来防止这种情况
const googlesignin =document.querySelector('#g-A-oauth')
googlesignin.addEventListener('click', e=>{
e.preventDefault()
admin.auth().getUserByEmail('exampşe@email.com')
.then(function(userRecord) {
//if user email exist do something
console.log('you already signin with wmail and password')
})
.catch(function(error) {
firebase.auth().signInWithPopup(provider).then(function(result) {
// This gives you a Google Access Token. You can use it to access the Google API.
var token = result.credential.accessToken;
// The signed-in user info.
var user = result.user;
// ...
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
});
})
关于javascript - Firebase 的“每个电子邮件地址一个帐户”允许 Google OAuth 在未经同意的情况下替换电子邮件/密码帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59806371/