检查Flutter App的Firebase Auth中是否已经存在电子邮件 [英] Check if an email already exists in Firebase Auth in Flutter App
问题描述
我目前正在开发一个Flutter应用,要求用户在使用前进行注册.我使用Firebase身份验证,并想检查应用程序中是否已经注册了电子邮件.
I'm currently developing a flutter app that requires users to register before using it. I use Firebase Authentication and would like to check whether an email is already registered in the app.
我知道简单的方法是在使用createUserWithEmailAndPassword()
方法时捕获异常(如
I know the easy way to do it is to catch the exception when using the createUserWithEmailAndPassword()
method (as answered in this question). The problem is that I ask for the email address in a different route from where the user is registered, so waiting until this method is called is not a good option for me.
我认为最好的选择是使用方法fetchProvidersForEmail()
,但是我似乎无法使其工作.
I think the best option would be to use the method fetchProvidersForEmail()
, but I can't seem to make it work.
如何使用该方法?还是有更好的选择来知道电子邮件是否已经注册?
How do I use that method? Or is there a better option to know if an email is already registered?
推荐答案
The error raised is a PlatformException so you can do something as follows-
try {
_firbaseAuth.createUserWithEmailAndPassword(
email: 'foo@bar.com',
password: 'password'
);
} catch(signUpError) {
if(signUpError is PlatformException) {
if(signUpError.code == 'ERROR_EMAIL_ALREADY_IN_USE') {
/// `foo@bar.com` has alread been registered.
}
}
}
Firebase Auth报告以下错误代码-
The following error codes are reported by Firebase Auth -
- ERROR_WEAK_PASSWORD-如果密码不够强.
- ERROR_INVALID_EMAIL-如果电子邮件地址格式错误.
- ERROR_EMAIL_ALREADY_IN_USE-如果该电子邮件已被其他帐户使用.
这篇关于检查Flutter App的Firebase Auth中是否已经存在电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!