AngularFire:数据仍然注销后访问数据、AngularFire

2023-09-14 00:24:38 作者:秋奈櫻舞

我与用户身份验证和授权AngularFire挣扎。

问题是,一旦授权用户登录并显示从火力地堡的数据,如果我注销,然后将数据仍然显示。我可以从范围分离的数据(删除$ scope.data ),这使得它不显示,但如果我再次登录了的未经授权用户,引用重新连接和数据仍然存在。

我想保留数据的客户端的某个地方。但我怎么能破坏呢?我试图通过一个新的,每次我退出更换火力地堡参考,徒劳的。

下面是code:

 <!DOCTYPE HTML>< HTML NG-应用=应用程序>< HEAD>  <标题>< /标题>< /头><机身NG控制器=CTRL>  < D​​IV NG隐藏=的loggedIn>未登录上述< / DIV>  <按钮NG隐藏=的loggedInNG点击=登录()>登录上述< /按钮>  < D​​IV>该数据:{{}数据}< / DIV>  <按钮NG秀=的loggedInNG点击=退出()>注销< /按钮>< /身体GT;<脚本类型=文/ JavaScript的SRC =bower_components /角/ angular.js>< / SCRIPT><脚本类型=文/ JavaScript的SRC =bower_components /火力/ firebase.js>< / SCRIPT><脚本类型=文/ JavaScript的SRC =bower_components /火力简单的登录/火力-简单login.js>< / SCRIPT><脚本类型=文/ JavaScript的SRC =bower_components / angularfire / angularfire.js>< / SCRIPT><脚本类型=文/ JavaScript的>angular.module('应用',['火力']).controller('CTRL',函数($范围,$火力点,$ firebaseSimpleLogin){  VAR REF =新的火力地堡(小于REF>);  VAR ngref = $火力点(REF);  VAR AUTH = $ firebaseSimpleLogin(REF);  $ scope.loggedIn = !! auth.user;  $ scope.login =功能(){    AUTH。$登录('谷歌'),然后(      功能(用户){        $ scope.data = ngref.name;        $ scope.loggedIn =真;      },      功能(错误){        抛出的错误;      });  };  $ scope.logout =功能(){    $ scope.loggedIn = FALSE;    。AUTH $注销();  };})< / SCRIPT>< / HTML> 

修改

看来,这个行为已经无关AngularFire。低于code使用的香草火力地堡API和结果是一样的。

  VAR AUTH =新FirebaseSimpleLogin(REF,功能(错误,用户){  如果(错误){    抛出的错误;  }否则如果(用户){    $范围。$应用(函数(){      $ scope.loggedIn =真;      $ scope.data = REF;    });  }其他{    $范围。$应用(函数(){      删除$ scope.data;      $ scope.loggedIn = FALSE;    });  }});$ scope.loggedIn = FALSE;$ scope.login =功能(){  auth.login('谷歌');};$ scope.logout =功能(){  auth.logout();}; 
4个数据驱动用户增长的痛点背后,有3套最优解 今天就聊这个

解决方案

我现在有点晚了(近一年),但是这可以通过删除的localStorage的入门固定的火力点:会话::。

  window.localStorage.removeItem(火力点:会话::<主机名称>); 

I'm struggling with user authentication and authorization with AngularFire.

The issue is that once an authorized user is logged in and the data from Firebase is displayed, if I log out then the data is still displayed. I can detach the data from the scope (delete $scope.data) which makes it not display, but then if I log in again as an unauthorized user, the reference is reattached and the data is still there.

I guess the data is retained client-side somewhere. But how can I destroy it? I have tried replacing the Firebase reference by a new one each time I log out, in vain.

Here is the code:

<!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>

EDIT

It seems that this behavior has nothing to do with AngularFire. The code below uses the vanilla Firebase API and the result is the same.

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

解决方案

I'm a bit late (almost a year), however this can be fixed by removing the localStorage entry for "firebase:session::".

window.localStorage.removeItem("firebase:session::<host-name>");