Android 上的 Firebase:如何检查 Firebase 身份验证失败的原因?

Firebase on Android: How to check Firebase auth failed reason?

我在 Android 和 Firebase 身份验证功能上使用 Firebase。

我尝试 FirebaseAuth.signInWithEmailAndPassword 万一失败我想知道登录过程失败的原因?

signInWithEmailAndPassword方法有addOnFailureListenerAPI。 我可以在 onFailure 回调方法中捕获 Exception(也许 FirebaseAuthException)。

auth.signInWithEmailAndPassword(loginRequest.id, loginRequest.password)
  .addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
      if (e instanceof FirebaseAuthException) {
        ((FirebaseAuthException) e).getErrorCode());
      }
    }
  });

我想知道登录过程失败的原因。在 onFailure.

我觉得可以这样操作:

  1. e 实例类型检查(e instanceOf FirebaseAuthInvalidUserException or FirebaseAuthInvalidCredentialsException or ,)
  2. e.getErrorCode()

我不想输入支票(它很脏)。
我更喜欢上面选择2中的方式。但是我找不到 e.getErrorCode() return 值集合的定义。 例如 ERROR_INVALID_EMAILERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL 等。 (它们在哪里定义的?)

如何找出 Firebase 身份验证失败的原因?

我在 Firebase 库中发现了一些代码,这些代码类似于目前看到的失败消息。但是还没试过。你可以试试看。

    ("ERROR_INVALID_CUSTOM_TOKEN", "The custom token format is incorrect. Please check the documentation."));
    ("ERROR_CUSTOM_TOKEN_MISMATCH", "The custom token corresponds to a different audience."));
    ("ERROR_INVALID_CREDENTIAL", "The supplied auth credential is malformed or has expired."));
    ("ERROR_INVALID_EMAIL", "The email address is badly formatted."));
    ("ERROR_WRONG_PASSWORD", "The password is invalid or the user does not have a password."));
    ("ERROR_USER_MISMATCH", "The supplied credentials do not correspond to the previously signed in user."));
    ("ERROR_REQUIRES_RECENT_LOGIN", "This operation is sensitive and requires recent authentication. Log in again before retrying this request."));
    ("ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL", "An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address."));
    ("ERROR_EMAIL_ALREADY_IN_USE", "The email address is already in use by another account."));
    ("ERROR_CREDENTIAL_ALREADY_IN_USE", "This credential is already associated with a different user account."));
    ("ERROR_USER_DISABLED", "The user account has been disabled by an administrator."));
    ("ERROR_USER_TOKEN_EXPIRED", "The user\'s credential is no longer valid. The user must sign in again."));
    ("ERROR_USER_NOT_FOUND", "There is no user record corresponding to this identifier. The user may have been deleted."));
    ("ERROR_INVALID_USER_TOKEN", "The user\'s credential is no longer valid. The user must sign in again."));
    ("ERROR_OPERATION_NOT_ALLOWED", "This operation is not allowed. You must enable this service in the console."));
    ("ERROR_WEAK_PASSWORD", "The given password is invalid."));

我也有同样的疑问,我在他们的文档中找到了更多信息。

例如,在您使用此方法 createUserWithEmailAndPassword 时,您可以在他们的 documentation page 上看到以下例外情况:

例外情况:

如果密码不够强则抛出 FirebaseAuthWeakPasswordException 如果电子邮件地址格式错误,则抛出 FirebaseAuthInvalidCredentialsException 如果已存在具有给定电子邮件地址的帐户,则抛出 FirebaseAuthUserCollisionException

每个异常都有一个文档页面,例如 FirebaseAuthUserCollisionException

的文档页面

在这里您可以看到不同的错误类型:

ERROR_EMAIL_ALREADY_IN_USE when trying to create a new account with createUserWithEmailAndPassword(String, String) or to change a user's email address, if the email is already in use by a different account

ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL when calling signInWithCredential(AuthCredential) with a credential that asserts an email address in use by another account. This error will only be thrown if the "One account per email address" setting is enabled in the Firebase console (recommended).

ERROR_CREDENTIAL_ALREADY_IN_USE when trying to link a user with an AuthCredential corresponding to another account already in use

因此,如果您执行 getErrorCode() 并将字符串与这些常量中的任何一个进行比较,您就会确切地知道异常的原因。

希望对您有所帮助。

成功启用电子邮件+密码身份验证方法后,您需要为您的 Firebase 项目创建一个应用程序才能正常工作。

转到: 概述 -> 添加另一个应用程序 -> 根据您的需要执行后续步骤。

希望对大家有所帮助。干杯!

过去需要将字符串转换为正确的错误消息,但自 firebase-admin 7.3.0 以来,这是由 AuthErrorHandler 完成的,您应该能够调用 FirebaseAuthException.getMessage()

如果您仍想构建它,FirebaseAuthException.getAuthErrorCode() 将 return AuthErrorCode 的一个实例,它现在是一个枚举。