5

I'm trying to store data in my services similar to the answer in :

Processing $http response in service

app.factory('myService', function($http) {
  var promise;
  var myService = {
    async: function() {
      if ( !promise ) {
        // $http returns a promise, which has a then function, which also returns a promise
        promise = $http.get('test.json').then(function (response) {
          // The then function here is an opportunity to modify the response
          console.log(response);
          // The return value gets picked up by the then in the controller.
          return response.data;
        });
      }
      // Return the promise to the controller
      return promise;
    }
  };
  return myService;
});

app.controller('MainCtrl', function( myService,$scope) {
  $scope.clearData = function() {
    $scope.data = {};
  };
  $scope.getData = function() {
    // Call the async method and then do stuff with what is returned inside our own then function
    myService.async().then(function(d) {
      $scope.data = d;
    });
  };
});

However, I noticed that the data in my services persists even after logout. Thus I could login as a completely different user and see data which I should not see.

How can I clear data after logout? Sure I could manually clear everything in all my services, but I am looking for a more general approach such as "clear all user data". I have tried to force a page refresh, and it works, but I don't like the flash it produces.

edit: Code from example

braX
  • 11,506
  • 5
  • 20
  • 33
user1354603
  • 4,034
  • 4
  • 31
  • 43
  • _how_ exactly "similar" though? (i.e. show your code) If you are doing it right, you really shouldn't have any persistent data in the service anyway. – thescientist May 05 '14 at 13:36
  • Can you share your service code? – Chandermani May 05 '14 at 13:38
  • I should rephrase my question, with logout I mean my own logout-function which just removes some user data. Services are still instantiated and containing the same stuff as before logout. – user1354603 May 05 '14 at 13:43
  • The example you've referenced doesn't save the data. How are you saving the data? Through cache? In a var on the service? – haimlit May 05 '14 at 14:19
  • As a promise/variable on the service, I'll add the code from the example to the question. – user1354603 May 05 '14 at 15:30
  • Services are singletons. Unless you exit the program they will continue to exist and maintain the data they hold. It is up to you to manage that data. Generally services are used to talk to the back end but if you're using them as a variable store you've got to manage it. – mortsahl May 05 '14 at 17:19
  • Thank you for the answer, I need to figure out a way to manage my data. I am using the services to talk to the backend, but I am storing the data retrieved from the backend in the services in order to bring down the number of requests. – user1354603 May 06 '14 at 07:13
  • how did you solve this problem? me too facing the same issue – Prithvi May 12 '16 at 07:47
  • This was a while ago, but I remember that the solution was as simple as just storing all data which needs to be cleared in the same place. You will probably need to create a "dataservice" or something similar to it. – user1354603 May 12 '16 at 08:27
  • I'm having the same issue, and hoping not to require a major re-write to solve this. – Matt Aug 15 '16 at 16:04

2 Answers2

0

To clear the data in myService, you may have a destroy() function that is called upon $scope.clearData. For example:

app.factory('myService',function('$http'){
   /// do you stuff
   return{
     myServices : //...,
     destroy : function(){
       promise = null;//destroy promise
     }
   }
});

On a side note, you probably don't want to store data in $scope. You may want to separate controlling and data.

Benson Zhang
  • 135
  • 1
  • 1
  • 9
0

I'm using angular-devise to manage my users. Here's what I have in my service to clear data on logout:

app.service("MyService", ["$rootScope", function($rootScope) {
  // destroy the user data on logout
  self = this;
  this.shouldBeDestroyed = "foo";

  $rootScope.$on('devise:logout', function(event) {
    self.shouldBeDestroyed = null;
  });
});

I'd love to find a more reliable way to destroy sensitive objects in services. This solved my problem though.

Matt
  • 5,800
  • 1
  • 44
  • 40