最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Create custom error messages for Firebase Authentication - Stack Overflow

programmeradmin0浏览0评论

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.

Any ideas? Thanks!

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.

Any ideas? Thanks!

Share Improve this question asked Jun 23, 2015 at 23:01 cfly24cfly24 1,9623 gold badges27 silver badges61 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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

发布评论

评论列表(0)

  1. 暂无评论