gpt4 book ai didi

android - 如何处理 FirebaseAuth 异常

转载 作者:太空宇宙 更新时间:2023-11-03 13:47:03 24 4
gpt4 key购买 nike

我正在使用 Firebase 的身份验证服务,它可以正常工作,但我不知道如何处理 createUserWithEmailAndPassword() 的错误代码,例如 auth/email-already-in-use 或 auth/invalid-email ,这里可以看到错误列表https://firebase.google.com/docs/reference/js/firebase.auth.Auth#createUserWithEmailAndPassword

public void register(View target){
EditText email = (EditText) findViewById(R.id.editTextName);
EditText pass = (EditText) findViewById(R.id.editTextPass);
Log.d("email",email.getText().toString());
Log.d("pass",pass.getText().toString());
auth.createUserWithEmailAndPassword(email.getText().toString(),pass.getText().toString())
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>(){
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(RegistroActivity.this, "success",
Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(RegistroActivity.this, "fail",
Toast.LENGTH_SHORT).show();
}

}
});

}

最佳答案

FirebaseAuth.getInstance().createUserWithEmailAndPassword("EMAIL", "PASSWORD")
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
if (task.getException() instanceof FirebaseAuthUserCollisionException) {
// thrown if there already exists an account with the given email address
} else if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
// thrown if the email address is malformed
} else if (task.getException instanceof FirebaseAuthWeakPasswordException) {
// thrown if the password is not strong enough
}
}
}
});

关于android - 如何处理 FirebaseAuth 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40138696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com