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

yuk*_*312 16 android firebase firebase-authentication

Firebase在Android和Firebase Auth功能上使用.

我试着FirebaseAuth.signInWithEmailAndPassword,万一它失败了,我想知道"为什么signIn过程失败?"

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

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

我想知道SignIn进程失败的原因.在onFailure.

我认为可以这样做:

  1. e实例类型检查(e instanceOf FirebaseAuthInvalidUserExceptionFirebaseAuthInvalidCredentialsExceptionor ,,,)
  2. e.getErrorCode()

我不想打字检查(它很脏).
我更喜欢上面选择的方式.但我找不到e.getErrorCode()返回值集合的定义.例如ERROR_INVALID_EMAIL,ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL等等(它们在哪里定义?)

请告诉我哪个是检查原因的最佳方法Firebase auth failed.

谢谢.

ugu*_*gur 12

我在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."));
Run Code Online (Sandbox Code Playgroud)

  • 似乎来自 https://github.com/firebase/firebase-android-sdk/blob/93c8b48269e42d1f5f116d7a54ad55d7dfa4c9fd/firebase-common/src/main/java/com/google/firebase/FirebaseError.java (3认同)

Lan*_*lot 9

我有同样的疑虑,我发现了更多关于他们文档的信息.

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

例外:

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

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

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

ERROR_EMAIL_ALREADY_IN_USE当试图创建一个新帐户createUserWithEmailAndPassword(字符串,字符串)或更改用户的电子邮件地址,如果电子邮件已在使用中通过调用signInWithCredential(AuthCredential)时,不同的帐户ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL与在断言电子邮件地址的凭据由另一个帐户使用.如果在Firebase控制台中启用了"每个电子邮件地址一个帐户"设置(推荐),则只会抛出此错误.尝试将用户与已使用的其他帐户对应的AuthCredential链接时出现ERROR_CREDENTIAL_ALREADY_IN_USE

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

希望能帮助到你.