What is the best practice in clearing service data for AngularJS. For example if user1 logs out and a user2 logs back in on the same browser, if I am not clearing the service data, then user2 will be using the same service data as user1.
For example in the code below, if user1 added more users to the service and logged out, and in the same browser, if user2 logged back in, they would be able to see all the users user1 added.
var module = angular.module('myapp', []);
module.service('userService', function(){
var users = ['John', 'James', 'Jake'];
});
I have two solutions to tackle this problem but none are ideal.
- Manually reset every single property in the service. This does not seem to be an ideal solution because someone might forget to do that someday.
- Reload window on logout. Seems to reset the service but it is somewhat poor user experience.
Is there a better alternative?