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);
                }
            }
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值