Firebase简单登录$ logout函数不会将user属性设置为null

时间:2014-04-26 19:25:03

标签: angularjs firebase angularfire firebase-security

根据$ firebaseSimpleLogin的$ logout方法的文档,API清楚地说明了

  

调用logout时,将触发$ firebaseSimpleLogin:logout事件,并且对象上的用户>属性将设置为null。

然而,它似乎并不适合我。

以下是我的代码片段:

  var firebaseUrl = 'https://MYURL.firebaseio.com/';
  var firebaseObject = new Firebase(firebaseUrl);

  $rootScope.firebaseLoginObject = $firebaseSimpleLogin(firebaseObject, function(error, user) {
    if (error) {
      console.log(error);
    } else if (user) {
      console.log('User ID: ' + user.uid + ', Provider: ' + user.provider);
    } else {
      console.log('User is logged out');
      // user is logged out
    }
  });

  // Function for logging out.
  $scope.logout = function() {
    $rootScope.firebaseLoginObject.$logout();
    console.log('i logged out successfully');
    console.log($rootScope.firebaseLoginObject);
    console.log($rootScope.firebaseLoginObject.user);
    console.log($rootScope.user);
    $location.path('/');
  };

点击" ng-click = logout()"后我在控制台中获得的输出表明$ rootScope.firebaseLoginObject未设置为null。

1 个答案:

答案 0 :(得分:1)

问题在于$ scope.logout。在$ logout函数完成其工作之前,您的console.log语句正在执行。

  $scope.logout = function() {
    $rootScope.firebaseLoginObject.$logout();

    // This is not necessarily true. At this point, you may still be
    // logged in since $logout can take an unknown amount of time to finish.

    console.log('i logged out successfully');
    console.log($rootScope.firebaseLoginObject);
    console.log($rootScope.firebaseLoginObject.user);
    console.log($rootScope.user);
    $location.path('/');
  };

要确保在$ logout完成后记录firebaseLoginObject的状态,请等到$ rootScope上的logout事件被广播。

  $rootScope.$on("$firebaseSimpleLogin:logout", function(event) {
    // Log values here.
  });