9

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

Hugh Hou
  • 2,344
  • 5
  • 31
  • 55
  • are you doing anything using "$onAuth"? Maybe in your auth factory? – jbdev Sep 25 '15 at 00:43
  • Yes I do. But using onAuth still won't know what I should close when logout. As my instances are everywhere. And most of them I can't close when leave view since they all need to listen to update from firebase. – Hugh Hou Nov 19 '15 at 17:53

3 Answers3

2

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();
});
Hardik Sondagar
  • 4,347
  • 3
  • 28
  • 48
  • This can be a solution. But using $rootscope just seem a little hacky. – Hugh Hou Feb 01 '16 at 15:56
  • Using $rootScope you can broadcast logout event in all the controllers. – Hardik Sondagar Feb 02 '16 at 06:53
  • If you destroy your firebase references, you need some way to reinitialize your factory/services/controllers where your references are defined. If you don't, no data once you logout and log back in. – Will Lovett Apr 06 '17 at 02:05
0

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

  • All - I had the same issue and the quick fix was to update the rules {".read": true}, only for read access, somewhat similar post you can get an idea to resolve it in a simple way http://stackoverflow.com/questions/14296625/restricting-child-field-access-with-security-rules There are some more examples to resolve this by writing a decorator and destroy all firebase references created with $firebaseArray and $firebaseObject. – Sunil Kumar Jan 26 '17 at 16:42
-1

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