Facebook
From Big Ostrich, 7 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 327
  1. angular.module('authentication',[])
  2.     .service('AuthService',['$http','TokenStorage','$rootScope',
  3.         function($http, TokenStorage, $rootScope) {
  4.             var that = this;
  5.             var authenticated = false;
  6.             var userId = null;
  7.             var username = {};
  8.  
  9.             that.getAuthenticated = function () {
  10.                 if(TokenStorage.retrieve()!== null)
  11.                     return true;
  12.                 else
  13.                     return false;
  14.             };
  15.  
  16.             that.getUserId = function () {
  17.                 return userId;
  18.             };
  19.  
  20.  
  21.             that.init = function () {
  22.                 return $http.get(urlBase +'user/current').success(function (user) {
  23.                     if(user.username !== 'anonymousUser'){
  24.                         username = user.username;
  25.                         userId = user.id;
  26.                         communeId = user.communeId;
  27.                         authenticated = true;
  28.                         $rootScope.$broadcast('auth.loggedin');
  29.                     }
  30.                     else
  31.                     {
  32.                         username = {};
  33.                         userId = null;
  34.                         authenticated = false;
  35.                         communeId = null;
  36.                         TokenStorage.clear();
  37.                         $rootScope.$broadcast('auth.loggedout');
  38.                     }
  39.                 });
  40.  
  41.  
  42.             };
  43.  
  44.             that.login = function (logdata) {
  45.                 return $http.post(urlBase +'login',logdata)
  46.                     .success(function (data, status, headers, config) {
  47.                         authenticated = true;
  48.                         $rootScope.$broadcast('auth.loggedin');
  49.                         console.log(headers('X-AUTH-TOKEN'));
  50.                         TokenStorage.store(headers('X-AUTH-TOKEN'));
  51.  
  52.                     }).error(function () {
  53.                         authenticated = false;
  54.                         userId = null;
  55.                         communeId = null;
  56.                         $rootScope.$broadcast('auth.loggedout');
  57.                     });
  58.             };
  59.  
  60.             that.logout = function () {
  61.                 TokenStorage.clear();
  62.                 authenticated = false;
  63.                 userId = null;
  64.                 communeId = null;
  65.                 $rootScope.$broadcast('auth.loggedout');
  66.  
  67.             };
  68.  
  69.         }])
  70.     .factory('TokenStorage', function() {
  71.         var storageKey = 'auth_token';
  72.         return {
  73.             store : function(token) {
  74.                 return localStorage.setItem(storageKey, token);
  75.             },
  76.             retrieve : function() {
  77.                 return localStorage.getItem(storageKey);
  78.             },
  79.             clear : function() {
  80.                 return localStorage.removeItem(storageKey);
  81.             }
  82.         };
  83.     });
  84.