I am currently wondering how it is possible to gain the error code after carrying out an unsuccessful login using Firebase
. From their legacy code, that you can see in this link below:
https://www.firebase.com/docs/android/guide/user-auth.html#section-handling-errors
@Override
public void onAuthenticationError(FirebaseError error) {
switch (error.getCode()) {
case FirebaseError.USER_DOES_NOT_EXIST:
// handle a non existing user
break;
case FirebaseError.INVALID_PASSWORD:
// handle an invalid password
break;
default:
// handle other errors
break;
}
}
You are provided an onAuthenticationError
, where the FirebaseError
can then be specifically analysed to produce a different feedback error to the user. However as they have recently released their new API, I have started to work with that. Here is the code I have to utilise now:
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
//If authentication fails
if (!task.isSuccessful()) {
// Handle the specific individual errors such as incorrect passwords
}
}
Unfortunately I am not sure how I am able to gather the specific error code from the Task<AuthResult>
object. I understand that I can gather the Exception
and Toast
this message, although I would prefer to carry out a switch on a proper error code rather than work with a String
explaining the error that occurred.
Usefirebase. auth. Error. code to get the specific error code.
To send a password reset email to user, on the Users page, hover over the user and click ... > Reset password. The user will receive an email with instructions on how to reset their password.
Firebase provides email and password authentication without any overhead of building backend for user authentication. Step 2: Go to Firebase console (http://console.firebase.google.com/) navigate to your application, and under the authentication tab, enable email/pass authentication. This is your sign-up activity.
you have to add the onFailureListner()
with the code below you can get the error code :
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof FirebaseAuthInvalidCredentialsException) {
notifyUser("Invalid password");
} else if (e instanceof FirebaseAuthInvalidUserException) {
String errorCode =
((FirebaseAuthInvalidUserException) e).getErrorCode();
if (errorCode.equals("ERROR_USER_NOT_FOUND")) {
notifyUser("No matching account found");
} else if (errorCode.equals("ERROR_USER_DISABLED")) {
notifyUser("User account has been disabled");
} else {
notifyUser(e.getLocalizedMessage());
}
}
}
});
notifyUser() method you should develop to show Toast or snackbar or dialog maybe
If your code already has onCompleteListener then I won't recommend you to add onFailureListner just get information about the exception because it will increase the number of listeners. You can get an error code and message as follow.
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
//If authentication fails
if (!task.isSuccessful()) {
String message = task.getException().getMessage();
String localizedMessage = task.getException().getLocalizedMessage();
String errorCode = ((FirebaseAuthInvalidUserException) task.getException()).getErrorCode();
}
}
I will recommend using Localized Message instead of just Message because a Localized message is short and has enough the information necessary to tackle the error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With