Advertisement
Guest User

adsf

a guest
Mar 3rd, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Twitter.factory('AuthService', AuthService);
  2. AuthService.$inject = ['$cookies', '$http'];
  3.  
  4. function AuthService($cookies, $http) {
  5.  
  6.     var AuthService = {
  7.         getAuthenticatedAccount: getAuthenticatedAccount,
  8.         isAuthenticated: isAuthenticated,
  9.         login: login,
  10.         logout: logout,
  11.         register: register,
  12.         setAuthenticatedAccount: setAuthenticatedAccount,
  13.         unauthenticate: unauthenticate
  14.     };
  15.  
  16.     return AuthService;
  17.  
  18.     function getAuthenticatedAccount() {
  19.         if (!$cookies.authenticatedAccount) {
  20.             return;
  21.         }
  22.  
  23.         return JSON.parse($cookies.authenticatedAccount);
  24.     }
  25.  
  26.     function isAuthenticated() {
  27.         return !!$cookies.authenticatedAccount;
  28.     }
  29.  
  30.     function login(email, password) {
  31.         return $http.post('/api/auth/login/', {
  32.             email: email,
  33.             password: password
  34.         }).then(loginSuccessFn, loginErrorFn);
  35.  
  36.         function loginSuccessFn(data, status, headers, config) {
  37.             AuthService.setAuthenticatedAccount(data.data);
  38.             window.location = '/';
  39.         }
  40.  
  41.         function loginErrorFn(data, status, headers, config) {
  42.             console.log(data.data);
  43.         }
  44.     }
  45.  
  46.     function logout() {
  47.         return $http.post('/api/auth/logout/')
  48.             .then(logoutSuccessFn, logoutErrorFn);
  49.  
  50.         function logoutSuccessFn(data, status, headers, config) {
  51.             AuthService.unauthenticate();
  52.             window.location = '/';
  53.         }
  54.  
  55.         function logoutErrorFn(data, status, headers, config) {
  56.             alert("Logout error.");
  57.         }
  58.     }
  59.  
  60.     function register(email, password, username) {
  61.         return $http.post('/api/accounts/', {
  62.             email: email,
  63.             password: password,
  64.             username: username,
  65.         }).then(registerSuccessFn, registerErrorFn);
  66.  
  67.         function registerSuccessFn(data, status, headers, config) {
  68.             AuthService.login(email, password);
  69.         }
  70.  
  71.         function registerErrorFn(data, status, headers, config) {
  72.             alert("Registration error.");
  73.         }
  74.     }
  75.  
  76.     function setAuthenticatedAccount(account) {
  77.         $cookies.authenticatedAccount = JSON.stringify(account);
  78.     }
  79.  
  80.     function unauthenticate() {
  81.         delete $cookies.authenticatedAccount;
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement