【问题标题】:Firebase logout show Permission denied error.Firebase 注销显示权限被拒绝错误。
【发布时间】:2015-08-25 02:23:21
【问题描述】:

所以每当我从 Firebase 注销时,我都会被耦合

Error: permission_denied: Client doesn't have permission to access the desired data.

我知道这是因为登录会话已终止,我的某些对象无法再访问 firebase 数据。但是如何在注销之前断开这些对象?

对于我的一个 Ionic View 中的注销按钮,它只是调用一个 firebase 服务:

function logout() {
      auth.$unauth();
      getCurrentUser();
};
function getCurrentUser() {
        var authData = auth.$getAuth();
        if (authData) {
            $rootScope.userId = authData.uid;
            $rootScope.currentUser = $firebaseObject(authRef.child("users").child(authData.uid));
            return $rootScope.currentUser;
        } else {
          console.log("User is not login!");
          $rootScope.userId = null;
          $location.path("/auth/signin");
          if ($rootScope.currentUser) {
            $rootScope.currentUser.$destroy();
          } 
        }
    };

所以我在那里销毁 $rootScope.currentUser。我对个人资料页面使用相同的 getCurrentUser。所以错误没有以这种方式出现。但是在其他视图中,我有另一个 $firebaseArray,还有另一个 Ref.on("child_added", function(snap) 具有相同的 $firebaseObject。当我查看个人资料页面时,此页面至少有 3 个 firebase 连接, 注销时出现 3 个 permission_denied 错误(注销按钮位于用户个人资料页面上)。

我的问题是,我如何在注销之前断开此 Firebase 连接?有没有办法断开所有的 Firebase 连接——无论是 AngularFire 还是普通的 Firebase?所以我可以注销而不用担心我还没有关闭哪个firebase连接?另外,由于注销按钮在配置文件范围内,而其他连接在不同的范围内,我不知道如何关闭甚至不在配置文件范围内的连接......

【问题讨论】:

  • 你在用“$onAuth”做什么吗?也许在你的认证工厂?
  • 是的,我愿意。但是使用 onAuth 仍然不知道注销时应该关闭什么。因为我的实例无处不在。他们中的大多数在离开视图时我无法关闭,因为他们都需要收听来自 firebase 的更新。

标签: angularjs firebase angularfire firebase-authentication


【解决方案1】:

您需要销毁之前保存数据的对象的 firebase ref。怎么样?

之前,我初始化了我的 var 歌曲,例如: this.songs = this.af.list('/songs');

当我 signOut() 时,我应该销毁我初始化的变量的引用以便我执行:

this.songs.$ref.off();

有了这一行,你的问题

【讨论】:

    【解决方案2】:

    您需要在注销时销毁所有 firebase 引用。 像这样。

    退出功能中。

    function logout() {
           auth.$unauth();
           $rootScope.$broadcast('logout');
    };
    

    在控制器中

    vm.profile = authService.profile(user.uid); // FirebaseObject Reference
    vm.parties = partyService.getPartiesByUser(user.uid); // FirebaseArray Reference
    
    $rootScope.$on('logout', function () {
      vm.parties.$destroy();
      vm.profile.$destroy();
    });
    

    【讨论】:

    • 这可能是一个解决方案。但是使用 $rootscope 似乎有点 hacky。
    • 使用 $rootScope 您可以在所有控制器中广播注销事件。
    • 如果你破坏了你的 firebase 引用,你需要一些方法来重新初始化你定义了引用的工厂/服务/控制器。如果不这样做,则在您注销并重新登录后将没有数据。
    【解决方案3】:

    好吧,我猜你有一个退出按钮。 所以在你的函数 logout() 你首先 $destroy 数据对象,以某种方式等待(这是我试图弄清楚的最佳实践),然后是 authref.unauth(); 我会说

    【讨论】:

    • 所有 - 我遇到了同样的问题,快速解决方法是更新规则 {".read": true},仅用于读取访问,有点类似的帖子你可以得到解决它的想法一个简单的方法stackoverflow.com/questions/14296625/… 有更多的例子可以通过编写一个装饰器来解决这个问题,并销毁所有使用 $firebaseArray 和 $firebaseObject 创建的 firebase 引用。
    猜你喜欢
    • 2016-09-25
    • 1970-01-01
    • 2019-04-16
    • 2016-10-30
    • 1970-01-01
    • 2013-12-16
    • 2018-07-29
    • 2021-10-05
    • 2020-02-15
    相关资源
    最近更新 更多