Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function () {
- 'use strict';
- angular
- .module('app', [])
- .factory('authService', AuthService);
- function AuthService($http, $localStorage) {
- return {
- getJWT: getJWT,
- login: login,
- isLogged: isLogged,
- logout: logout
- };
- function getJWT() {
- $http.get('/')
- .success(function (response) {
- console.log(response);
- if (response.data.Authorization !== '') {
- return response.data.Authorization;
- }
- });
- }
- function login(jwt) {
- // Gets the user through the JSON Web Token
- $http.defaults.headers.common.Authorization = 'Bearer ' + jwt;
- $http.post('http://api.cocoon.dev/user')
- .success(function (response) {
- if (response.account_type != 'employer') {
- // Save user data in the local storage
- $localStorage.currentUser = {
- token: jwt,
- first_name: response.profile.first_name
- };
- callback(true);
- } else {
- this.logout();
- callback(false);
- }
- })
- }
- function isLogged() {
- return $localStorage.get('currentUser') !== null;
- }
- function logout() {
- delete $localStorage.currentUser;
- $http.defaults.headers.common.Authorization = '';
- }
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement