其他分享
首页 > 其他分享> > Firebase获得登陆失败或注册账号已存在等提示信息createUserWithEmailAndPassword(email, password)

Firebase获得登陆失败或注册账号已存在等提示信息createUserWithEmailAndPassword(email, password)

作者:互联网

项目场景:

本文使用的Android客户端和JAVA代码 ,使用Firebase的登录/注册,并没有提示账户已存在或登录密码错误等其他错误信息


问题描述:

在异常描述中有提示 The email address is already in use by another account.

那么如何才能获取到这个异常的错误信息


APP 中接收数据代码:

2021-06-30 15:39:19.655 24299-24299/test.colin.game W/LoginActivity: FireBasecreateUserWithEmail:failure
    com.google.firebase.auth.FirebaseAuthUserCollisionException: The email address is already in use by another account.
        at com.google.android.gms.internal.firebase-auth-api.zzto.zza(com.google.firebase:firebase-auth@@21.0.1:25)
        at com.google.android.gms.internal.firebase-auth-api.zzuw.zza(com.google.firebase:firebase-auth@@21.0.1:9)
        at com.google.android.gms.internal.firebase-auth-api.zzux.zzl(com.google.firebase:firebase-auth@@21.0.1:1)
        at com.google.android.gms.internal.firebase-auth-api.zzuu.zzk(com.google.firebase:firebase-auth@@21.0.1:25)
        at com.google.android.gms.internal.firebase-auth-api.zztl.zzh(com.google.firebase:firebase-auth@@21.0.1:1)
        at com.google.android.gms.internal.firebase-auth-api.zzoc.zza(com.google.firebase:firebase-auth@@21.0.1:2)

这里是官方给的代码例子用于创建firebase用户的方法createUserWithEmailAndPassword 

mAuth.createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    // Sign in success, update UI with the signed-in user's information
                    Log.d(TAG, "createUserWithEmail:success");
                    FirebaseUser user = mAuth.getCurrentUser();
                    updateUI(user);
                } else {
                    // If sign in fails, display a message to the user.
                    Log.w(TAG, "createUserWithEmail:failure", task.getException());
                    Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
                            Toast.LENGTH_SHORT).show();
                    updateUI(null);
                }
            }
        });


解决方案:

要得到提示账户已存在或登录密码错误等其他错误信息,这些错误信息其实已经在抛出的异常中给出了,只要在task.getException()处改成task.getException().getMessage()增加一个getMessage()方法即可得到这些信息

mAuth.createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    // Sign in success, update UI with the signed-in user's information
                    Log.d(TAG, "createUserWithEmail:success");
                    FirebaseUser user = mAuth.getCurrentUser();
                    updateUI(user);
                } else {
                    // If sign in fails, display a message to the user.
                    Log.w(TAG, "createUserWithEmail:failure", task.getException());
                    //Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
                     //       Toast.LENGTH_SHORT).show();

                    //task.getException().getMessage()即可得到错误信息
                     Toast.makeText(LoginActivity.this,task.getException().getMessage(),
                                Toast.LENGTH_SHORT).show();

                    updateUI(null);
                }
            }
        });

标签:google,firebase,auth,task,Firebase,user,提示信息,password,com
来源: https://blog.csdn.net/gsrkuang/article/details/118363844