AngularFire(Angular + Firebase)身份验证错误延迟

时间:2015-07-05 15:50:10

标签: javascript angularjs firebase angularfire firebase-authentication

我发现了一些奇怪的东西。请帮忙!

$scope.login = function() {
    ref.authWithPassword({
        email: $scope.user.email,
        password: $scope.user.password
    }, function(error, authData) {
        if (error) {
            console.log("Login Failed!", error);
            $scope.message = error.toString();
        } else {
            $location.path('/meetings');
            console.log("Authenticated successfully with payload:", authData);
        }
    });
} //login

这是一个登录功能,它运行良好。但是,我提交登录后3秒,我得到error。我注意到在{{message}}收到价值后我的$scope.message没有立即更新。我认为Angular一旦发生变化就应该显示出价值。 ?

我第二次点击后,显示错误。

这是我打印值的地方:

<p class="error formerror" ng-show="message">{{message}}</p>

1 个答案:

答案 0 :(得分:1)

您正在调用authWithPassword,这是Firebase常规JavaScript SDK的一部分。此API将启动身份验证过程,并在身份验证完成时调用您的函数。不幸的是,AngularJS不再了解您对$scope所做的任何更新。

要让AngularJS了解更新,请将您的代码包装在$timeout电话中:

$scope.login = function() {
    ref.authWithPassword({
        email: $scope.user.email,
        password: $scope.user.password
    }, function(error, authData) {
        $timeout(function() {
            if (error) {
                console.log("Login Failed!", error);
                $scope.message = error.toString();
            } else {
                $location.path('/meetings');
                console.log("Authenticated successfully with payload:", authData);
            }
        });
    });
} //login

请注意,正是由于这个原因,AngularFire在其$firebaseAuth服务中围绕这些身份验证功能提供了便利包装。请参阅section in the AngularFire guide on logging users in

您要查找的包装函数为$authWithPassword,请阅读how to use $authWithPassword in its API documentation的示例。