![](/img/trans.png)
[英]Firebase auth check for email already exists without using signInWithEmailAndPassword
[英]Get Firebase User.ProviderID on auth/wrong-password when using signInWithEmailAndPassword
我有一个使用 Firebase 进行身份验证的应用程序。
我允许用户使用 Google/Facebook/Twitter 或使用 Email 和密码登录。
此外,我已在 Firebase 内激活,用户只能在每个 email 创建一个帐户。
我想介绍以下案例:
用户使用 facebook 注册并获得使用其 facebook email 创建的用户帐户(例如facebookuser@gmail.com
)。
几天后,用户返回应用程序,但忘记了他使用 facebook 注册并尝试使用他们的 email 地址facebookuser@gmail.com
和他们常用的密码登录。
firebase.auth().signInWithEmailAndPassword(email, password)
方法抛出auth/wrong-password
错误,因为使用 facebook 登录方法没有给出密码,但 email 确实存在。
而不是显示无用的“密码错误或email对应的账户没有设置密码”。 错误 我想改为检查哪个提供商用于注册并提醒用户改为使用提供商登录。
不幸的是,似乎不存在一种方法来获取给定 email 的 User.ProviderID 或了解是否因为用户输入了错误的密码或没有首先给出密码而给出了auth/wrong-password
错误代替用户使用 OAuthProvider 注册。
好的,我错过了fetchProvidersForEmail
方法。
仅供参考:(不是我将使用的最终代码)
signInWithEmailAndPassword
检查用户是否存在createUserWithEmailAndPassword
创建它如果错误是auth/wrong-password
它会使用fetchProvidersForEmail
检查用户是否使用了提供商进行注册
firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) { var errorCode = error.code; var errorMessage = error.message; // User not found? Create user. if ( errorCode === 'auth/user-not-found' ) { firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) { // Handle Errors here. var errorCode = error.code; var errorMessage = error.message; if ( errorCode == 'email-already-in-use' ) { alert('You already have an account with that email.'); } else if ( errorCode == 'auth/invalid-email' ) { alert('Please provide a valid email'); } else if ( errorCode == 'auth/weak-password' ) { alert('The password is too weak.'); } else { alert(errorMessage); } console.log(error); }); // Wrong Password Error } else if ( errorCode === 'auth/wrong-password' ) { // Check if User has signed up with a OAuthProvider firebase.auth().fetchProvidersForEmail(email).then(function( result ){ // … show OAuthProvider Login Button }); alert('Wrong password. Please try again'); } else { alert( errorMessage ); } console.log( error ); });
这是个问题,在 github @angularfire lib 上报告这个问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.