Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2015
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function ()
  2. {
  3.     'use strict';
  4.  
  5.     angular.module('app.core')
  6.             .factory('AuthService', AuthService);
  7.  
  8.     /** @ngInject */
  9.     function AuthService($http, $state, store, token, API, $q) {
  10.         var session;
  11.         return {
  12.             login: login,
  13.             logout: logout,
  14.             isLoggedIn: isLoggedIn,
  15.             getUserData: getUserData,
  16.             loadSessionData: loadSessionData,
  17.             getSessionData: getSessionData,
  18.             setSessionData: setSessionData,
  19.             destroySessionData: destroySessionData
  20.         }
  21.  
  22.         /** @ngInject */
  23.         function login(form){
  24.             $http({
  25.                     url: API.URL+'auth/login',
  26.                     skipAuthorization: true,
  27.                     method: 'POST',
  28.                     data: {
  29.                         email: form.email,
  30.                         password: form.password
  31.                     }
  32.                 })
  33.             .success(function(response)
  34.             {
  35.                 store.set(token.auth, response.token);
  36.                 $state.go('app.dashboard');
  37.             })
  38.             .error(function(err){
  39.                 console.log(err);
  40.             });
  41.         }
  42.  
  43.         /** @ngInject */
  44.         function logout() {
  45.             if (store.get(token.auth)) {
  46.                 store.remove(token.auth);
  47.             }
  48.             destroySessionData();
  49.             $state.go('app.pages_auth_login');
  50.         };
  51.  
  52.         /** @ngInject */
  53.         function isLoggedIn(user){
  54.             return true;
  55.         }
  56.  
  57.         /** @ngInject */
  58.         function loadSessionData() {
  59.             $http({
  60.                     url: API.URL+'auth/session-data',
  61.                     method: 'GET'
  62.                 })
  63.             .success(function(response)
  64.             {
  65.                 setSessionData(response);
  66.             })
  67.             .error(function(err){
  68.                 console.log(err);
  69.             });
  70.         };
  71.  
  72.         /** @ngInject */
  73.         function destroySessionData() {
  74.             setSessionData(null);
  75.         }
  76.  
  77.         /** @ngInject */
  78.         function isLoggedIn(user){
  79.             return true;
  80.         };
  81.  
  82.         /** @ngInject */
  83.         function getUserData(){
  84.             if (session) {
  85.                 return session.user;
  86.             }
  87.             return null;
  88.         }
  89.  
  90.         /** @ngInject */
  91.         function getSessionData() {
  92.             return session;
  93.         };
  94.  
  95.         /** @ngInject */
  96.         function setSessionData(data){
  97.             session = data;
  98.         };
  99.     }
  100. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement