我正在使用FirebaseAuth创建用户帐户,但是我想知道用户是否已经注册,以提醒用户正在发生的事情。
我从4月开始就收到有关此内容的帖子,但似乎 addOnCompleteListener 不再存在。
还有另一种实现方法吗?
这就是我所拥有的:
try {
final newUser = await _auth.createUserWithEmailAndPassword(email: _email, password: _password);
if (newUser != null)
Navigator.pushNamed(context, HomePage.id);
} catch (e) {...}
答案 0 :(得分:3)
要知道Firebase身份验证中是否已存在给定电子邮件地址的帐户,请致电fetchSignInMethodsForEmail
method。这种方法的逻辑是,它将告诉您为该电子邮件地址创建了一个帐户的提供商,以便您可以显示一个允许用户选择其提供商的UI。
答案 1 :(得分:1)
FirebaseAuth的createUserWithEmailAndPassword函数将返回一个异常(如果已经有与该电子邮件关联的帐户)以及注册期间发生的任何其他错误。您可以侦听此异常并采取相应措施。我通常的方法是在单独的Auth服务类的异步函数中:
异步功能示例:
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函数供参考:
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.';
}
}