检查电子邮件是否已存在于 Flutter App 的 Firebase Auth 中 [英] Check if an email already exists in Firebase Auth in Flutter App

查看:22
本文介绍了检查电子邮件是否已存在于 Flutter App 的 Firebase Auth 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆