Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create custom error messages for Firebase Authentication

I am programming an Ionic app and have decided to use Firebase to simplify authentication. I have set up the username and password login and it works fine. The way I have it currently set up, if there is an error during login (like misspelled password), then the error will be displayed in the logs. Now, this error isn't very pretty and not what I would like the end user to see. I have been trying to figure out how to create a custom error message for different events so the user will see, "Password is Incorrect" instead of "Error: The specified password is incorrect.". In that example, its not much different, but you get the point.

Here is a sample of the login code:

$scope.login = function(username, password) {
    var fbAuth = $firebaseAuth(fb);
    fbAuth.$authWithPassword({
        email: username,
        password: password
    }).then(function(authData) {
        $location.path('/tab')
    }).catch(function(error) {
        console.error("ERROR: " + error);
    });
}

There is an error code for each error, but I'm not sure how to harness that to create a custom error message based on the error.

Here's an example

Any ideas? Thanks!

like image 731
cfly24 Avatar asked Oct 29 '25 01:10

cfly24


1 Answers

The error is embedded in error.code.

Then write a seperate function that catched this error and displays a message:

function(error){
switch (error.code)
      case "INVALID_USER":
           $scope.message = "custom message"
       // etc
  }

You can find the list of possible error codes in the firebase documentation here: API documentation

like image 193
JohnAndrews Avatar answered Oct 30 '25 14:10

JohnAndrews



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!