Firebase logout show Permission denied error.

2.8k views Asked by At

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

3

There are 3 answers

1
user3704232 On

well, i guess you have a button to logout. so in your function logout() you'd first $destroy the data object, somehow wait (whichs' best practice i'm trying to figure out), and then authref.unauth(); i'd say

3
Hardik Sondagar On

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();
});
0
Nacho Sizle On

You need destroy the firebase ref for the object that you saved data previously. How?

Before, I initialize my var songs like: this.songs = this.af.list('/songs');

When I signOut(), I should destroy the reference of the variable that I initialized so that I execute:

this.songs.$ref.off();

With this line, your problem