我是Firebase开发的新手。我正在使用Firebase创建用户登录系统。我正在努力验证注册用户的电子邮件。这是我写的代码。
auth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Toast.makeText(MainActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
user.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Log.i("Success", "Yes");
}
else{
Log.i("Success", "No");}
}
});
if (!task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(MainActivity.this, Activity2.class));
finish();
}
}
});
}
});
编辑:注册成功后不会触发验证电子邮件。我得到的错误是
10-12 10:41:47.579 10529-10529 / com.firebase I /成功:
Nocom.google.firebase.FirebaseException:内部错误有 发生了。 [USER_NOT_FOUND]
答案 0 :(得分:0)
答案 1 :(得分:0)
在获取当前用户并致电createUserWithEmailAndPassword()
之前,您需要检查sendEmailVerification()
的状态以确保其成功。 createUserWithEmailAndPassword()
可能失败的原因有多种(帐户已存在,密码较弱,电子邮件地址格式不正确等)。如果失败,则当前用户为先前已登录的用户,如果之前没有登录,则为null。
重构您的代码:
auth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Toast.makeText(MainActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
if (!task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
user.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Log.i("Success", "Yes");
}
else{
Log.i("Success", "No");}
}
});
startActivity(new Intent(MainActivity.this, Activity2.class));
finish();
}
}
});