为Firebase身份验证创建自定义错误消息

时间:2015-06-23 23:01:50

标签: javascript angularjs firebase ionic

我正在编写一个Ionic应用程序并决定使用Firebase来简化身份验证。我已经设置了用户名和密码登录,它工作正常。我当前设置的方式,如果在登录过程中出现错误(如拼写错误的密码),则错误将显示在日志中。现在,这个错误不是很漂亮,而不是我希望最终用户看到的。我一直试图弄清楚如何为不同的事件创建自定义错误消息,以便用户将看到“密码不正确”而不是“错误:指定的密码不正确。”。在那个例子中,它没有太大的不同,但你明白了。

以下是登录代码示例:

$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);
    });
}

每个错误都有错误代码,但我不确定如何根据错误创建自定义错误消息。

Here's an example

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:4)

错误嵌入在error.code中。

然后编写一个单独的函数来捕获此错误并显示一条消息:

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

您可以在firebase文档中找到可能的错误代码列表:API documentation

相关问题