firebaseAuthenticator.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
if (task.getException() instanceof FirebaseAuthUserCollisionException) {
Toast.makeText(SignUpActivity.this, "User with this email already exist.", Toast.LENGTH_SHORT).show();
}
} else {
sendVerificationEmail();
startActivity(new Intent(SignUpActivity.this, DetailsCaptureActivity.class));
}
// ...
}
});
这就是诀窍发生的地方:
if (task.getException() instanceof FirebaseAuthUserCollisionException) {
Toast.makeText(SignUpActivity.this,
"User with this email already exist.", Toast.LENGTH_SHORT).show();
8 回答
要检测具有该电子邮件地址的用户是否已存在,您可以检测
createUserWithEmailAndPassword ()
的调用何时因auth/email-already-in-use
而失败 . 我看到@Srinivasan刚刚发布了一个答案 .或者,您可以通过调用fetchSignInMethodsForEmail()(以前称为fetchProvidersForEmail())来检测已使用的电子邮件地址 .
当用户尝试使用相同的电子邮件地址创建用户时,任务响应将为 "Response: The email address is already in use by another account."
首先,您需要确保在Firebase控制台中启用了该限制(帐户和电子邮件地址设置) . 看看@ Srinivasan的回答 .
然后,在您的Java代码中执行此操作:
这就是诀窍发生的地方:
使用电子邮件和密码注册用户时可能会抛出几个异常,但我们感兴趣的是
FirebaseAuthUserCollisionException
. 顾名思义,如果电子邮件已经存在,则抛出此异常 . 如果抛出的异常是此类的实例,请让用户知道 .作为@Frank在这里回答的做法是使用
fetchProvidersForEmail()
的代码我正在调查这种情况,我们可以检测用户是否存在,并执行注册和登录 .
fetchProvidersForEmail
现在是最好的选择 . 我找到了 this 教程 . 希望它也能帮到你!将以下代码添加到 MainActivity.java 文件 . 当用户尝试使用相同的电子邮件地址注册时,将弹出一条消息"The email address is already used by another account"作为 Toast
You do not have to do anything because the backend of Firebase will do the job.
除非您指的是重新验证应用程序 . 以一个例子为例,w