如何知道用户的电子邮件是否已经注册 [英] How to know if user's email is already registered

查看:267
本文介绍了如何知道用户的电子邮件是否已经注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FirebaseAuth创建用户帐户,但是我想知道用户是否已经注册,以提醒用户正在发生的事情.

I'm using FirebaseAuth to create an user account, but I want to know if the user is already registered, to alert the user what's happening.

我从4月开始看到有关此内容的帖子,但似乎 addOnCompleteListener 不再存在.

I've seen posts about this from April but it seems like addOnCompleteListener doesn't exists anymore.

还有另一种方法可以实现吗?

Is there another way to implement this?

这就是我所拥有的:

try {
  final newUser = await _auth.createUserWithEmailAndPassword(email: _email, password: _password);
  if (newUser != null) 
    Navigator.pushNamed(context, HomePage.id);
} catch (e) {...}

推荐答案

FirebaseAuth的createUserWithEmailAndPassword函数将返回异常(如果已经存在与该电子邮件关联的帐户)以及注册期间发生的任何其他错误.您可以侦听此异常并采取相应措施.我通常的做法是在单独的Auth服务类的异步函数中:

FirebaseAuth's createUserWithEmailAndPassword function will return an exception if there is already an account associated with that email, and for any other error that happens during signup. You can listen for this exception and act accordingly. The way I usually do this is in an async function in a seperate Auth service class:

异步功能示例:

Future<String> signUp(String email, String password) async {
    AuthResult result = await _auth.createUserWithEmailAndPassword(
        email: email, password: password);
    return result.user.uid;
  }

等待结果:

try {
        await signUp(email, password).then((uid) {
          // User successfully created so Navigate to new page etc
        });
      } catch (e) {
        print("Error in sign up: $e");
        String exception = getExceptionText(e);
        _showErrorAlert(
          title: "Signup failed",
          content: exception,
        );
      }

下面是Auth服务中的getExceptionText函数供参考:

And for reference here is the getExceptionText function inside the Auth service:

String getExceptionText(Exception e) {
    if (e is PlatformException) {
      switch (e.message) {
        case 'There is no user record corresponding to this identifier. The user may have been deleted.':
          return 'User with this e-mail not found.';
          break;
        case 'The password is invalid or the user does not have a password.':
          return 'Invalid password.';
          break;
        case 'A network error (such as timeout, interrupted connection or unreachable host) has occurred.':
          return 'No internet connection.';
          break;
        case 'The email address is already in use by another account.':
          return 'Email address is already taken.';
          break;
        default:
          return 'Unknown error occured.';
      }
    } else {
      return 'Unknown error occured.';
    }
  }

这篇关于如何知道用户的电子邮件是否已经注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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