Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function () {
  2.     'use strict';
  3.  
  4.     angular
  5.         .module('app', [])
  6.         .factory('authService', AuthService);
  7.  
  8.     function AuthService($http, $localStorage) {
  9.         return {
  10.             getJWT: getJWT,
  11.             login: login,
  12.             isLogged: isLogged,
  13.             logout: logout
  14.         };
  15.  
  16.         function getJWT() {
  17.             $http.get('/')
  18.                 .success(function (response) {
  19.                     console.log(response);
  20.                     if (response.data.Authorization !== '') {
  21.                         return response.data.Authorization;
  22.                     }
  23.                 });
  24.         }
  25.  
  26.         function login(jwt) {
  27.             // Gets the user through the JSON Web Token
  28.             $http.defaults.headers.common.Authorization = 'Bearer ' + jwt;
  29.             $http.post('http://api.cocoon.dev/user')
  30.                 .success(function (response) {
  31.                     if (response.account_type != 'employer') {
  32.                         // Save user data in the local storage
  33.                         $localStorage.currentUser = {
  34.                             token: jwt,
  35.                             first_name: response.profile.first_name
  36.                         };
  37.  
  38.                         callback(true);
  39.                     } else {
  40.                         this.logout();
  41.                         callback(false);
  42.                     }
  43.                 })
  44.         }
  45.  
  46.  
  47.         function isLogged() {
  48.             return $localStorage.get('currentUser') !== null;
  49.         }
  50.  
  51.         function logout() {
  52.             delete $localStorage.currentUser;
  53.             $http.defaults.headers.common.Authorization = '';
  54.         }
  55.     }
  56. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement