Prevent items in scope from writing to a different user's records

前端 未结 2 1210
名媛妹妹
名媛妹妹 2021-01-03 15:52

I was having success with using AngularFire in a scenario where there is one user on my application.

Now that I have authentication up and running, I\'m noticing tha

相关标签:
2条回答
  • 2021-01-03 16:56

    This is a result of the implicit data binding remaining intact as you switch users. If the new user shows up and creates a new binding, it will consider the existing data to be local changes that it should assimilate (that's why you see the original user's items being added to the new user), but if you try to clear them first without releasing the binding then you are implicitly telling Firebase to delete that data from the original user's item list (also not what you want). So you need to release the data bindings when you detect the logout (or login) events as needed.

    The callback in the angularFire promise provides an "unbind" method (see here and here):

    var promise = angularFire(ref, $scope, 'items');
    promise.then(function(unbind){
      // Calling unbind() will disassociate $scope.items from Firebase
      // and generally it's useful to add unbind to the $scope for future use.
    });
    

    You have a few idiosyncrasies in your code that are likely causing it not to work, and remember that unbind won't clear the local collection for you. But just so you have an idea of how it should work (and to prove it does work) here is a fiddle.

    0 讨论(0)
  • 2021-01-03 16:57

    You need to unbind $scope.items on logout. The best way to do this will be to save the unbind function given to your promise in $scope:

    var ref = new Firebase('https://<my-firebase>.firebaseio.com/items/[user1id]');
    angularFire(ref, $scope, 'items').then(function(unbind) {
      $scope.unbindItems = unbind;
    });
    
    $scope.$on('angularFireAuth:logout', function() {
      $scope.unbindItems();
    });
    
    0 讨论(0)
提交回复
热议问题