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

新手上路,请多包涵

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

我尝试 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 FirebaseAuthInvalidUserExceptionFirebaseAuthInvalidCredentialsException 或,,,)
  2. e.getErrorCode()

我不想输入支票(它很脏)。

我更喜欢上面选择2中的方式。但是我找不到 e.getErrorCode() 返回值集合的定义。例如 ERROR_INVALID_EMAILERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL 等(它们在哪里定义?)

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

原文由 yuki312 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 680
2 个回答

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

例如,在您使用此方法 createUserWithEmailAndPassword 时,您可以在其 文档页面上 看到以下异常:

例外情况:

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

对于每个异常,还有一个文档页面,例如 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() 并将字符串与这些常量中的任何一个进行比较,您就会确切地知道异常的原因。

希望能帮助到你。

原文由 Lancelot 发布,翻译遵循 CC BY-SA 4.0 许可协议

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

原文由 ugur 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题