检查Flutter App中Firebase Auth中是否已存在电子邮件

问题描述 投票:1回答:2

我目前正在开发一个颤动应用程序,要求用户在使用之前进行注册。我使用Firebase身份验证,并希望检查是否已在应用中注册了电子邮件。

我知道这样做的简单方法是在使用createUserWithEmailAndPassword()方法时捕获异常(如this question中所述)。问题是我要求用户注册的路径与用户注册的路径不同,所以等到这个方法被调用对我来说不是一个好选择。

我认为最好的选择是使用方法fetchProvidersForEmail(),但我似乎无法使它工作。

我该如何使用该方法?或者有没有更好的选择来知道电子邮件是否已经注册?

firebase firebase-authentication flutter
2个回答
1
投票

我认为应用程序内唯一的可能性是尝试使用该电子邮件登录(signInWithEmailAndPassword)并检查结果。

如果密码无效,则该帐户存在。如果帐户无效,则该帐户不存在。

Error 17011
There is no user record corresponding to this identifier. The user may have been deleted
Error 17009
The password is invalid or the user does not have a password

由于这是一种丑陋的解决方案,您可以使用它来验证此附加调用,以检查电子邮件格式是否正确(根据firebase规则)。如果它不符合它将抛出一个address is badly formatted,你可以尽快提醒用户。

您可以使用当前版本的插件使用错误代码进行这些检查。


0
投票

提出的错误是PlatformException所以你可以做如下事情 -

try {
  _firbaseAuth.createUserWithEmailAndPassword(
    email: '[email protected]', 
    password: 'password'
  );
} catch(signUpError) {
  if(signUpError is PlatformException) {
    if(signUpError.code == 'ERROR_EMAIL_ALREADY_IN_USE') {
      /// `[email protected]` has alread been registered.
    }
  }
}

Firebase Auth报告以下错误代码 -

  • ERROR_WEAK_PASSWORD - 如果密码不够强。
  • ERROR_INVALID_EMAIL - 如果电子邮件地址格式错误。
  • ERROR_EMAIL_ALREADY_IN_USE - 如果该电子邮件已被其他帐户使用。
© www.soinside.com 2019 - 2024. All rights reserved.