【问题标题】:AngularFire (Angular + Firebase) Authentication Error delayAngularFire(Angular + Firebase)身份验证错误延迟
【发布时间】:2015-07-05 15:50:10
【问题描述】:

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

$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、4 秒得到了error。我注意到我的 {{message}} 在收到 $scope.message 的价值后并没有立即更新。我认为 Angular 应该在它发生变化时立即显示该值。 ?

第二次点击后,出现错误提示。

这是我打印值的地方:

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

【问题讨论】:

    标签: javascript angularjs firebase angularfire firebase-authentication


    【解决方案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 的示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-06
      • 2016-09-19
      • 1970-01-01
      • 2019-12-07
      • 2017-12-09
      • 2017-06-18
      • 2019-05-02
      相关资源
      最近更新 更多