Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase logout show Permission denied error.

So whenever I logout from Firebase, I got coupled

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

I understand it is because login session is terminated, some of my objects cannot access firebase data any more. But how can I disconnect this objects before logout?

For logout button in one of my Ionic View, it just call a firebase service:

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

So I destroy the $rootScope.currentUser there. I use the same getCurrentUser for profile page. So the Error did not show up this way. But when in other views, which I have another $firebaseArray, and also another Ref.on("child_added", function(snap) with the same $firebaseObject. When I view the profile page, then this page with at least 3 firebase connection, I got 3 permission_denied Errors when I logout (logout button is on user profile page).

My question is, how do I disconnect this firebase connection before I logout? Is there a way disconnect ALL the firebase connection - no matter AngularFire or regular Firebase? So I can logout without worry about which firebase connection I have no close yet? Also, since the Logout button is in Profile scope and the others connection is in a different scope, I have no idea how to close the connection which is not even in the profile scope...

like image 941
Hugh Hou Avatar asked Jun 10 '15 02:06

Hugh Hou


1 Answers

You need to destroy all the firebase references on logout. Something like this.

In logout function.

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

In controller

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();
});
like image 86
Hardik Sondagar Avatar answered Nov 09 '22 14:11

Hardik Sondagar