Guest User

Untitled

a guest
Apr 24th, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. service('actionService',function ($window, sessionService) {
  2.    this.redirectNotLogged = function () {
  3.    if(!sessionService.get('id')){
  4.     $window.location.href = "some/url";
  5.    }
  6.    };
  7. });
  8. service('sessionService',function ($window, userService) {
  9.  
  10.  this.initSession = function () {
  11.   usersService.getUserProfile()
  12.   .then(function(res){
  13.    sessionService.setSession(res);
  14.   });
  15.  };
  16.  
  17.  this.setSession = function (data) {
  18.   $window.localStorage['session'] = data;
  19.  };
  20.  
  21.  this.get = function (key) {
  22.   var session =  JSON.parse($window.localStorage.session);
  23.  
  24.   return session.data[key];
  25.  };
  26.  
  27. });
  28. service('userService',function (actionService) {
  29.   this.getUserProfile  = function () {
  30.  
  31.   return $http()
  32.   .success(function(res){
  33.     return res;
  34.   });
  35.  
  36.   };
  37.  
  38.   this.doSomethingElse = function () {
  39.    
  40.     actionService.redirectNotLogged();
  41.     //if session user
  42.     alert('you are logged in');
  43.   };
  44. });
  45.  
  46. controller('Ctrl',function(sessionService, userService){
  47.  
  48. sessionService.initSession();
  49.  
  50. userService.doSomethingElse();
  51.  
  52.  
  53. });
Advertisement
Add Comment
Please, Sign In to add comment