【问题标题】:Firebase on Android: How to check Firebase auth failed reason?Android 上的 Firebase:如何检查 Firebase 身份验证失败的原因?
【发布时间】:2016-10-28 12:39:41
【问题描述】:

我在 Android 上使用 Firebase 和 Firebase Auth 功能。

我尝试FirebaseAuth.signInWithEmailAndPassword,万一它失败了,我想知道为什么登录过程失败了?

signInWithEmailAndPassword 方法具有addOnFailureListener API。 我可以在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() 返回值集合的定义。 例如ERROR_INVALID_EMAILERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL 等。 (它们在哪里定义?)

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

【问题讨论】:

    标签: android firebase firebase-authentication


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      例如在你使用这个方法createUserWithEmailAndPassword你可以在他们的documentation page上看到以下异常:

      例外情况:

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

      对于每个异常,还有一个文档页面,例如FirebaseAuthUserCollisionException 的文档页面

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

      ERROR_EMAIL_ALREADY_IN_USE 尝试使用 createUserWithEmailAndPassword(String, String) 创建新帐户或更改用户的电子邮件地址(如果电子邮件已被其他帐户使用)时

      ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL 调用 signInWithCredential(AuthCredential) 时使用的凭据断言另一个帐户正在使用的电子邮件地址。只有在 Firebase 控制台中启用了“每个电子邮件地址一个帐户”设置(推荐)时,才会引发此错误。

      ERROR_CREDENTIAL_ALREADY_IN_USE 尝试将用户与已在使用的另一个帐户对应的 AuthCredential 关联时

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

      希望对你有帮助。

      【讨论】:

        【解决方案3】:

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

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

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

        【讨论】:

          【解决方案4】:

          我在 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."));
          

          【讨论】:

          • 这些错误代码是否有可用的公共静态字符串常量? (例如 FirebaseAuthUserCollisionException.ERROR_EMAIL_ALREADY_IN_USE)
          • 我不这么认为。正确的实施与@Lancelot 的回答一样。和你的第一个选择。单独处理每个异常并不脏而且更好。即使您选择第二个,您也将处理每个案例。看起来代码量一样。
          • 感谢您的信息,尽管我想与自己键入字符串值相比,声明字符串常量值更安全、更容易,因为如果已经声明了常量并且键入错误,自动完成将节省您的时间如果您需要自己编写这些字符串,则可能会发生。
          猜你喜欢
          • 2019-03-09
          • 2017-11-27
          • 1970-01-01
          • 2017-08-24
          • 1970-01-01
          • 2020-07-09
          • 2017-07-25
          • 2020-05-22
          相关资源
          最近更新 更多