daily pastebin goal
12%
SHARE
TWEET

Untitled

a guest Sep 23rd, 2016 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. discovrApp.factory('AuthenticationService', function (
  2.   $http,
  3.   $rootScope,
  4.   $localStorage,
  5.   jwtHelper,
  6.   apiURL){
  7.   var service = {};
  8.  
  9.   var config = {headers:  {
  10.       Authorization: '',
  11.           'Content-Type': 'application/json'
  12.       }
  13.   };
  14.  
  15.   service.Login = Login;
  16.   service.Logout = Logout;
  17.   service.SignUp = SignUp;
  18.  
  19.   service.Profile = Profile;
  20.   service.ChangePassword = ChangePassword;
  21.   service.ResetPassword = ResetPassword;
  22.   service.GetProfile = GetProfile;
  23.   //service.SignUp = SignUp;
  24.   //service.SignUp = SignUp;
  25.  
  26.  
  27.   return service;
  28.  
  29.   function Login(username,password,callback) {
  30.     $http.post(apiURL + 'api/rest/auth/login/', { username: username,   password: password })
  31.       .success(function(response){
  32.         //login successful if there's a token in the respose
  33.         if(response.token){
  34.           //decode token, to get the user id insert on payload
  35.           var token = jwtHelper.decodeToken(response.token);
  36.           var userProfile = 0;
  37.           userProfile = GetProfile(token.user_id);
  38.           console.log(userProfile);
  39.           //store username and token in local storage to keep user logged in between paga refreshes
  40.           $localStorage.currentUser = {id: token.user_id, username: username, token: response.token };
  41.           //config.headers.Authorization = 'JWT ' + response.token;
  42.           //add jwt token to auth header for all requests made by the $http services
  43.          $http.defaults.headers.common.Authorization = 'JWT ' + response.token;
  44.           //execuete callback with true to indicate successful login
  45.           callback(true);
  46.         }else{
  47.           //execute callback with false to indicate failed login
  48.           callback(false);
  49.         }
  50.       });
  51.   }
  52.  
  53.   function Logout() {
  54.     $http.post(apiURL + 'api/rest/auth/logout/')
  55.       .success(function(response){
  56.         //remove user from local storage and clear http auth header
  57.         delete $localStorage.currentUser;
  58.         localStorage.removeItem('user');
  59.         config.headers.Authorization = '';
  60.         $http.defaults.headers.common.Authorization = '';
  61.       });
  62.  
  63.   }
  64.  
  65.   function SignUp(callback,username,password1,password2,email,more){
  66.     $http.post(apiURL + 'api/rest/auth/registration/', { username: username, password1: password1, password2: password2, email: email})
  67.       .success(function(response){
  68.         //login successful if there's a token in the respose
  69.         if(response.token){
  70.          console.log(response.token);
  71.          //store username and token in local storage to keep user logged in between paga refreshes
  72.           $localStorage.currentUser = { username: username, token: response.token };
  73.           //add jwt token to auth header for all requests made by the $http services
  74.           $http.defaults.headers.common.Authorization = 'JWT ' + response.token;
  75.           //execuete callback with true to indicate successful login
  76.           callback(true);
  77.         }else{
  78.           //execute callback with false to indicate failed login
  79.           callback(false);
  80.         }
  81.       });
  82.   }
  83.  
  84.   function Profile(){
  85.     $http.get(apiURL + 'api/user/' + $localStorage.currentUser.id + '/').
  86.       then(function successCallback(response) {
  87.           localStorage.setItem('user', JSON.stringify(response.data));
  88.           console.log(response.data)
  89.       }, function errorCallback(response) {
  90.           console.log(response);
  91.      });
  92.   }
  93.  
  94.   function GetProfile(id){
  95.     $http.get(apiURL + 'api/user/' + id + '/').
  96.     then(function successCallback(response){
  97.       $rootScope.userProfile = response.data.Kind;
  98.       localStorage.setItem('profile', $rootScope.userProfile);
  99.       //console.log(($rootScope.userProfile = response.data.Kind));
  100.     });    
  101.     return $rootScope.userProfile;
  102.   }
  103. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top