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

标签 android firebase firebase-authentication

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

在尝试实现 Firebase 电子邮件/密码身份验证时,我决定在用户​​帐户创建和/或登录失败的情况下记录/toast,而不仅仅是 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 中获取错误,而不是在堆栈跟踪中。当帐户创建失败时,此代码有效并 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();


                    }

最佳答案

您的“变通办法”是正确的方法。获取用于测试和调试日志记录的文本字符串的正确方法是 task.getException().getMessage()。但是,该字符串可能不适合在 toast 中使用,因为在 SDK 版本 9.4.0 中,消息始终为英语。它不受设备区域设置的影响。

关于android - 仅在尝试检索 task.getResult() 时抛出 Firebase 电子邮件/密码身份验证异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39482463/

相关文章:

ios - Swift Firebase - 有没有办法直接设置 Auth.auth().currentUser && Auth.auth().currentUser?.uid

firebase - 如何在 Flutter 中为 Firebase 身份验证手动触发 onAuthStateChanged?

android - 在 Osmdroid 覆盖层上禁用 onClick 事件

java - CM Home 的 Android DashClock 扩展设置 (CyanogenMod 11)

android - 使用库更新应用程序后小部件消失

ios - 如何在 Firebase 中使用 "StorageReference.downloadURLWithCompletion()"?

android - Google Assistant SDK 可以与我自己的应用集成吗?

android - Firebase 云消息传递 API key

javascript - 函数返回未定义、预期的 Promise 或值

android - 如何使用 Firebase Auth 将 token 发送回 android