gpt4 book ai didi

android - 无法捕获 Firebase 异常

转载 作者:行者123 更新时间:2023-11-29 15:39:39 24 4
gpt4 key购买 nike

我刚刚创建了 SignIn(AppCompatActivity Activity, String email, String password) 方法来登录我的 Firebase Android 应用中的现有用户。它可以在正确的凭据下完美运行,但是当输入不正确的电子邮件和/或密码时,它无法捕获正确的异常。方法如下:

public static int SignIn(final AppCompatActivity activity, String email, String password) {
int resultTask = -2;
FirebaseUtil.mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
if (!FirebaseUtil.mAuth.getCurrentUser().isEmailVerified()) {
FirebaseUtil.mAuth.getCurrentUser().sendEmailVerification();
FirebaseUtil.mAuth.signOut();
resultTask = 1;
} else {
FirebaseUtil.isCustom = true;
resultTask = 2;
}
} else
resultTask = -1;
}
});
return resultTask;
}

当输入错误的凭据时,它返回 -2。该方法出了什么问题?

编辑(2)

答案在这里

我通过将方法类型从 int 更改为 void 解决了这个问题。原因如下:

在我的 Activity/fragment 中,我调用了类似的方法,

int result = SignInUpActivity(_params_);

请注意该方法的默认返回值(它是 -2,但在执行登录/注册等 Firebase 操作后,它可能会返回任何其他值)。在这行之后,我检查结果:

if (result == -2) {
// some stuff
}
else if (result == -1) {
// some other stuff
}
else if (result == 0) {
// ...
}
// and other value checks for result handling

此时,我没有给 Firebase 时间来获取/检查/执行其自己的身份验证方法。因此,SignInUpActivity() 返回其默认值 (-2)。为了彻底解决这个问题,我将方法类型从 int 更改为 void

我的最终代码如下所示:

public static void SignIn(final AppCompatActivity activity, final String email, final String password) {
FirebaseUtil.mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
/**
* Thrown when one or more of the credentials passed to a method fail to
* identify and/or authenticate the user subject of that operation.
* Inspect the error code and message to find out the specific cause.
* https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuthInvalidCredentialsException
*/
ProgressDialog.DismissProgressDialog();
AlertDialog.ShowAlertDialog(activity,
"Invalid credentials", "Check email and password again.",
true, "ok", null,
false, "", null,
false, -1,
true);
} else if (task.getException() instanceof FirebaseAuthInvalidUserException) {
/**
* Inspect the error code and message to find out the specific cause.
* https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuthInvalidUserException
*/
ProgressDialog.DismissProgressDialog();
AlertDialog.ShowAlertDialog(activity,
"Invalid credentials", "Check email and password again.",
true, "ok", null,
false, "", null,
false, -1,
true);
}
} else {
if (!FirebaseUtil.mAuth.getCurrentUser().isEmailVerified()) {
FirebaseUtil.mAuth.getCurrentUser().sendEmailVerification();
FirebaseUtil.mAuth.signOut();
ProgressDialog.DismissProgressDialog();
AlertDialog.ShowAlertDialog(activity,
"Email not verified", "To start working, please verify your email.",
true, "ok", null,
false, "", null,
true, R.drawable.ic_verified_user,
true);
} else {
FirebaseUtil.isCustom = true;
ProgressDialog.DismissProgressDialog();
}
}
}
});
}

public static void Register(final AppCompatActivity activity, final String email, final String password) {
FirebaseUtil.mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
if (task.getException() instanceof FirebaseAuthUserCollisionException) {
/**
* Thrown when an operation on a FirebaseUser instance couldn't be
* completed due to a conflict with another existing user.
* https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuthUserCollisionException
*/
ProgressDialog.DismissProgressDialog();
AlertDialog.ShowAlertDialog(activity,
"Try another", "This email has already registered.",
true, "ok", null,
false, "", null,
false, -1,
true);
} else {
ProgressDialog.DismissProgressDialog();
AlertDialog.ShowAlertDialog(activity,
"Whoops", "An error occurred. Try again later.",
true, "ok", null,
false, "", null,
false, -1,
true);
}
} else {
try {
FirebaseUtil.mAuth.getCurrentUser().sendEmailVerification();
FirebaseUtil.mAuth.signOut();
ProgressDialog.DismissProgressDialog();
AlertDialog.ShowAlertDialog(activity,
"The last step", "For verification, instructions sent to your email. Please, check your email.",
true, "ok", null,
false, "", null,
true, R.drawable.ic_verified_user,
true);
} catch (Exception e) {
ProgressDialog.DismissProgressDialog();
AlertDialog.ShowAlertDialog(activity,
"Whoops", "An error occurred. Try again later.",
true, "ok", null,
false, "", null,
false, -1,
true);
}
}
}
});
}

无论如何,问题解决了。

最佳答案

根据这个问题:How to catch a Firebase Auth specific exceptions

你应该检查

if (!task.isSuccessful())

但是你正在做

if (task.isSuccessful())

然后您可以在 try block 内抛出由 task.getException 返回的异常,并捕获您正在使用的方法可能抛出的每种类型的异常

关于android - 无法捕获 Firebase 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42889052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com