Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2015
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. app.factory("FlashService", function($rootScope) {
  3.   return {
  4.     show: function(message) {
  5.       $rootScope.flash = message;
  6.     },
  7.     clear: function() {
  8.       $rootScope.flash = "";
  9.     }
  10.   }
  11. });
  12.  
  13. app.factory("SessionService", function() {
  14.   return {
  15.     get: function(key) {
  16.       return sessionStorage.getItem(key);
  17.     },
  18.     set: function(key, val) {
  19.       return sessionStorage.setItem(key, val);
  20.     },
  21.     unset: function(key) {
  22.       return sessionStorage.removeItem(key);
  23.     }
  24.   }
  25. });
  26.  
  27. app.factory("AuthenticationService", function($http, $sanitize, $rootScope, SessionService, theUser, FlashService, CSRF_TOKEN) {
  28.  
  29.   var cacheSession   = function() {
  30.     SessionService.set('authenticated', true);
  31.     SessionService.set('username', 'terje');
  32.   };
  33.  
  34.   var uncacheSession = function() {
  35.     SessionService.unset('authenticated');
  36.     SessionService.unset('username');
  37.   };
  38.  
  39.   var loginError = function(response) {
  40.     FlashService.show(response.flash);
  41.   };
  42.  
  43.   var sanitizeCredentials = function(credentials) {
  44.     return {
  45.       email: $sanitize(credentials.email),
  46.       password: $sanitize(credentials.password),
  47.       csrf_token: CSRF_TOKEN
  48.     };
  49.   };
  50.  
  51.   return {
  52.     login: function(credentials) {
  53.       var login = $http.post("auth/login", sanitizeCredentials(credentials)).success(function(data) {
  54.         theUser = data;
  55.         $rootScope.theUser = theUser;
  56.        // console.log(theUser);
  57.       });
  58.       login.success(cacheSession);
  59.       login.success(FlashService.clear);
  60.       login.error(loginError);
  61.       return login;
  62.     },
  63.     logout: function() {
  64.       var logout = $http.get("auth/logout");
  65.       logout.success(uncacheSession);
  66.     theUser = [];
  67.     delete $rootScope.theUser;      
  68.       return logout;
  69.     },
  70.     isLoggedIn: function() {
  71.       return SessionService.get('authenticated');
  72.     }
  73.   };
  74. });
  75. app.controller("LogoutController", function($scope, $location, AuthenticationService) {
  76.   console.log("Logged out");
  77.   AuthenticationService.logout().success(function() {
  78.    $location.path('/test');
  79.   });
  80. });
  81. app.controller("LoginController", function($scope, $location, AuthenticationService) {
  82.   $scope.credentials = { email: "", password: "" };
  83.  
  84.   $scope.login = function() {
  85.     AuthenticationService.login($scope.credentials).success(function() {
  86.       $location.path('/test');
  87.     });
  88.   };
  89. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement