Blame view
app/partials/login/login.service.js
4.53 KB
4bb02bb84
|
1 2 3 4 5 6 7 8 9 10 11 12 |
(function(){ 'use strict'; angular.module('acufuel') .service('LoginService', ['$rootScope', '$q', '$http', '$state', 'BASE_URL', 'AUTH_EVENTS', 'USER_ROLES', LoginService]); function LoginService($rootScope, $q, $http, $state, BASE_URL, AUTH_EVENTS, USER_ROLES) { var authService = {}, username = '', role = '', isAuthenticated = false; var currentUserDetail = JSON.parse(window.localStorage.getItem("currentUser")); |
4bda5699a
![]() |
13 |
// console.log(currentUserDetail) |
4bb02bb84
|
14 |
if (currentUserDetail) { |
4bda5699a
![]() |
15 |
//console.log('aaya') |
4bb02bb84
|
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
if(currentUserDetail.type == 'ADMIN' || currentUserDetail.type == 'FBO'){ $rootScope.isAuthenticated = true; role = USER_ROLES.admin; }else if(currentUserDetail.type == 'FLIGHT_DEPT'){ $rootScope.isAuthenticated = false; role = USER_ROLES.user; } } authService.loginUser = function(data) { var deferred = $q.defer(); $http({ method : 'POST', url : BASE_URL.url +'/login', headers : {'Content-Type': 'application/x-www-form-urlencoded'}, data : data }) .then(function (result){ deferred.resolve(result.data); },function (result){ |
4bda5699a
![]() |
37 |
//console.log(result.data) |
4bb02bb84
|
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
deferred.resolve(result.data); }) return deferred.promise; } authService.authenticate = function() { var deferred = $q.defer(); $http({ method : 'GET', url : BASE_URL.url+'/user/authenticate', headers : {'Content-Type': 'application/x-www-form-urlencoded'} }).then(function (result){ if(result.data.userProfile.userType.type == 'ADMIN' || result.data.userProfile.userType.type == 'FBO' || result.data.userProfile.userType.type == 'FLIGHT_DEPT'){ authService.setUser(result.data); $state.go('app.dashboard') }else{ localStorage.clear(); toastr.info("Please check username and password"); } deferred.resolve(result.data); },function (result){ deferred.resolve(result.data); }); return deferred.promise; } |
dd378d69f
|
63 64 65 |
authService.setAuth = function(data) { localStorage.setItem('loginStatus', data); } |
4bb02bb84
|
66 67 |
authService.logout = function(data) { |
dd378d69f
|
68 69 70 |
localStorage.clear(); $rootScope.isAuthenticated = false; $rootScope.$broadcast(AUTH_EVENTS.updateUser); |
4bb02bb84
|
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
var deferred = $q.defer(); $http({ method : 'POST', url : BASE_URL.url +'/user/logout', headers : {'Content-Type': 'application/json'}, data : data }) .success(function(result) { deferred.resolve(result.data); }) return deferred.promise; } authService.getUser = function () { return JSON.parse(window.localStorage.getItem("currentUser")); } |
8f7dbe97c
![]() |
90 |
|
4bb02bb84
|
91 |
authService.setUser = function (authData) { |
8f7dbe97c
![]() |
92 |
console.log('authdata', authData); |
4bb02bb84
|
93 |
window.localStorage.setItem("currentUser", JSON.stringify(authData.userProfile.userType)); |
4bda5699a
![]() |
94 |
window.localStorage.setItem("userProfileId", JSON.stringify(authData.userProfile.id)); |
8f7dbe97c
![]() |
95 |
window.localStorage.setItem('currentUserName', JSON.stringify(authData.userProfile.firstName +' '+ authData.userProfile.lastName)); |
4bb02bb84
|
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
if(authData.userProfile.userType.type == 'ADMIN' || authData.userProfile.userType.type == 'FBO'){ role = USER_ROLES.admin; $rootScope.isAuthenticated = true; }else if(authData.userProfile.userType.type == 'FLIGHT_DEPT'){ role = USER_ROLES.user; $rootScope.isAuthenticated = false; } } authService.isAuthenticated = function () { return isAuthenticated; } authService.role = function () { return role; } authService.isAuthorized = function (authorizedRoles) { if (!angular.isArray(authorizedRoles)) { authorizedRoles = [authorizedRoles]; } return (authService.isAuthenticated() && authorizedRoles.indexOf(role) !== -1); } return authService; } })(); |