Android上的Firebase:如何检查Firebase身份验证失败的原因?
我在Android上使用Firebase和Firebase身份验证函数 我尝试使用电子邮件和密码登录FirebaseAuth.signIn,如果失败,我想知道登录过程失败的原因Android上的Firebase:如何检查Firebase身份验证失败的原因?,android,firebase,firebase-authentication,Android,Firebase,Firebase Authentication,我在Android上使用Firebase和Firebase身份验证函数 我尝试使用电子邮件和密码登录FirebaseAuth.signIn,如果失败,我想知道登录过程失败的原因 signInWithEmailAndPassword方法具有addOnFailureListenerAPI。 我可以在onFailure回调方法中捕获异常(可能是FirebaseAuthException) auth.signInWithEmailAndPassword(loginRequest.id, loginReq
signInWithEmailAndPassword
方法具有addOnFailureListener
API。
我可以在onFailure
回调方法中捕获异常
(可能是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
中
我认为可以这样做:
e
实例类型检查(e实例ofFirebaseAuthInvalidUserException
或FirebaseAuthInvalidCredentialsException
或,,,,,)e.getErrorCode()
我更喜欢选择2的方式。在上面但是我找不到
e.getErrorCode()
返回值集合的定义。
e、 g错误\u无效\u电子邮件
,错误\u帐户存在\u凭证不同
,等等。
(它们的定义在哪里?)
如何找出Firebase auth失败的原因?我在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您可以在其上看到以下异常: 例外情况: 如果密码不够强,则引发FirebaseAuthWeakPasswordException 如果电子邮件地址格式不正确,则引发FirebaseAuthInvalidCredentialsException 如果已存在具有给定电子邮件地址的帐户,则引发FirebaseAuthUserCollisionException 对于每个异常,都有一个文档页面,例如 在这里您可以看到不同的错误类型: 尝试使用createUserWithEmailAndPassword(字符串,字符串)创建新帐户或更改用户的电子邮件地址(如果电子邮件已由其他帐户使用)时,出现错误\u EMAIL\u已\u在\u使用中 当使用断言另一帐户正在使用的电子邮件地址的凭据调用signInWithCredential(AuthCredential)时,错误\u ACCOUNT\u存在\u,凭据不同。只有在Firebase控制台(推荐)中启用“每个电子邮件地址一个帐户”设置时,才会引发此错误 尝试将用户与另一个已在使用的帐户对应的AuthCredential链接时出错\u CREDENTIAL\u已在使用中 因此,如果执行getErrorCode()并将字符串与这些常量中的任何一个进行比较,您将确切知道异常的原因
希望有帮助。一旦成功启用电子邮件+密码身份验证方法,您将需要为Firebase项目创建一个应用程序 转到: 概述->添加另一个应用->根据需要执行以下步骤
希望对大家都有帮助。干杯 过去需要将字符串转换为正确的错误消息,但由于firebase admin 7.3.0是由执行此操作的,您应该能够调用
FirebaseAuthException.getMessage()
如果仍要生成此代码,
FirebaseAuthException.getAuthErrorCode()
将返回的实例,该实例现在是一个枚举。是否有可用于这些错误代码的公共静态字符串常量?(例如FirebaseAuthUserCollisionException.ERROR\u EMAIL\u已经在使用)我不这么认为。正确的实施如@Lancelot的回答所示。还有你的第一个选择。它不是肮脏的,最好分别处理每个异常。即使你选择第二个,你也要处理每一个案子。这似乎是同样多的代码。感谢您提供的信息,尽管我想声明字符串常量值比自己键入字符串值更安全、更容易,因为如果已声明常量,则自动完成将节省您的时间,如果您需要自己编写这些字符串,则可能会发生键入错误。似乎来自