AngularFire:注销后仍可访问数据

时间:2014-05-06 21:16:34

标签: angularjs firebase angularfire firebase-security

我正在努力使用AngularFire进行用户身份验证和授权。

问题是,一旦授权用户登录并显示来自Firebase的数据,如果我退出,则仍会显示数据。我可以从范围(delete $scope.data)中分离数据,这使得它不显示,但是如果我再次以未授权的用户身份登录,则重新附加引用并且数据仍然存在

我猜数据是保留在客户端的某个地方。但我怎么能摧毁它呢?我每次退出时都尝试用新的替换Firebase引用,但是徒劳无功。

以下是代码:

<!DOCTYPE html>
<html ng-app="app">
<head>
  <title></title>
</head>
<body ng-controller="ctrl">

  <div ng-hide="loggedIn">Not logged In</div>
  <button ng-hide="loggedIn" ng-click="login()">Log in</button>

  <div>The data: {{data}}</div>
  <button ng-show="loggedIn" ng-click="logout()">Log out</button>

</body>
<script type="text/javascript" src="bower_components/angular/angular.js"></script>
<script type="text/javascript" src="bower_components/firebase/firebase.js"></script>
<script type="text/javascript" src="bower_components/firebase-simple-login/firebase-simple-login.js"></script>
<script type="text/javascript" src="bower_components/angularfire/angularfire.js"></script>
<script type="text/javascript">

angular.module('app', ['firebase'])

.controller('ctrl', function ($scope, $firebase, $firebaseSimpleLogin) {
  var ref = new Firebase(<ref>);
  var ngref = $firebase(ref);
  var auth = $firebaseSimpleLogin(ref);
  $scope.loggedIn = !!auth.user;
  $scope.login = function () {
    auth.$login('google').then(
      function (user) {
        $scope.data = ngref.name;
        $scope.loggedIn = true;
      }, 
      function (error) {
        throw error;
      });
  };
  $scope.logout = function () {
    $scope.loggedIn = false;
    auth.$logout();
  };
})

</script>
</html>

修改

似乎这种行为与AngularFire无关。以下代码使用vanilla Firebase API,结果相同。

var auth = new FirebaseSimpleLogin(ref, function (error, user) {
  if (error) {
    throw error;
  } else if (user) {
    $scope.$apply(function () {
      $scope.loggedIn = true;
      $scope.data = ref;
    });
  } else {
    $scope.$apply(function () {
      delete $scope.data;
      $scope.loggedIn = false;
    });
  }
});

$scope.loggedIn = false;
$scope.login = function () {
  auth.login('google');
};
$scope.logout = function () {
  auth.logout();
};

1 个答案:

答案 0 :(得分:3)

我有点晚了(差不多一年),但是可以通过删除“firebase:session ::”的localStorage条目来解决这个问题。

@Entity
@Table(name = "T_GLOBAL_USERS_AUTHORIZATION")
public class TGlobalUsersAuthorization{
  ....
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "USER_ID", nullable = false)
  public TUserProfile getUserProfile() {
    return userProfile;
  }
}