仅在尝试检索task.getResult()时抛出Firebase电子邮件/密码身份验证异常

时间:2016-09-14 04:26:06

标签: android firebase firebase-authentication

只是我不理解task.getResult()应该如何运作?是否打算返回(或导致)异常?模糊失败消息的原因是安全原因吗?想阻止人们探索有关现有帐户的信息吗?

在尝试实施Firebase电子邮件/密码身份验证时,我决定在用户​​帐户创建和/或登录失败的情况下记录/烘烤的不仅仅是authorization failed

因此,我使用了以下代码,并将作业temp = "poop"替换为temp = task.getResult().toString()。这样,我可以在测试应用程序时获得更好的细节。但奇怪的是,当我拨打temp = task.getResult().toString()时,应用程序崩溃了。它报道:

FATAL EXCEPTION: main
Process: [classified], PID: 24461
com.google.android.gms.tasks.RuntimeExecutionException: com.google.firebase.auth.FirebaseAuthUserCollisionException: The email address is already in use by another account.
    at com.google.android.gms.tasks.zzh.getResult(Unknown Source)... etc etc etc etc

但只要我不调用task.getResult(),它就不会崩溃,即使发生相同的情况(尝试制作重复的帐户 )。

我想在toast中获取错误,而不是在堆栈跟踪中。帐户创建失败时,此代码可以正常工作"poop"

 mAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());

                    // If sign in fails, display a message to the user. If sign in succeeds
                    // the auth state listener will be notified and logic to handle the
                    // signed in user can be handled in the listener.
                    if (!task.isSuccessful()) {

                        String temp =  "poop"; // task.getResult().toString();

                        Toast.makeText(Sign_IN_Activity.this, temp,
                                Toast.LENGTH_SHORT).show();

                        // Toast.makeText(Sign_IN_Activity.this, R.string.auth_failed,
                        //        Toast.LENGTH_SHORT).show();
                    }

                    else if(task.isSuccessful()) {
                        Toast.makeText(Sign_IN_Activity.this, R.string.auth_succeeded,
                                Toast.LENGTH_SHORT).show();
                    }

                    // [START_EXCLUDE]
                    hideProgressDialog();
                    // [END_EXCLUDE]
                }
            });
    // [END create_user_with_email]
}

此代码崩溃,将task.getResult()放入堆栈跟踪而不是Toast中:

mAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());

                    // If sign in fails, display a message to the user. If sign in succeeds
                    // the auth state listener will be notified and logic to handle the
                    // signed in user can be handled in the listener.
                    if (!task.isSuccessful()) {

                        String temp =  task.getResult().toString();

                        Toast.makeText(Sign_IN_Activity.this, temp,
                                Toast.LENGTH_SHORT).show();

                        // Toast.makeText(Sign_IN_Activity.this, R.string.auth_failed,
                        //        Toast.LENGTH_SHORT).show();
                    }

                    else if(task.isSuccessful()) {
                        Toast.makeText(Sign_IN_Activity.this, R.string.auth_succeeded,
                                Toast.LENGTH_SHORT).show();
                    }

                    // [START_EXCLUDE]
                    hideProgressDialog();
                    // [END_EXCLUDE]
                }
            });
    // [END create_user_with_email]
}

我做了一点解决方法(?):

if (!task.isSuccessful()) {

                        String temp = "auth failed";
                        try {
                            temp =  task.getException().getMessage();
                        } catch (Exception e) {

                        }
                        Toast.makeText(Sign_IN_Activity.this, temp,
                                Toast.LENGTH_SHORT).show();


                    }

1 个答案:

答案 0 :(得分:4)

你的&#34;解决问题&#34;是正确的方法。获取测试和调试日志记录的文本字符串的正确方法是task.getException().getMessage()。但是,该字符串可能不适合在toast中使用,因为在SDK版本9.4.0中,该消息始终为英语。它不受设备区域设置的影响。