Blame view
app/partials/login/login.service.js
4.72 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 |
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){ |
67d43a8c6
|
35 |
deferred.resolve(result); |
4bb02bb84
|
36 |
},function (result){ |
67d43a8c6
|
37 |
deferred.resolve(result); |
4bb02bb84
|
38 39 40 41 42 43 44 45 46 47 48 |
}) 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){ |
c277275bb
|
49 |
if(result.data.userProfile.userType.type == 'ADMIN' || result.data.userProfile.userType.type == 'FBO'){ |
4bb02bb84
|
50 51 |
authService.setUser(result.data); $state.go('app.dashboard') |
c277275bb
|
52 53 54 |
}else if(result.data.userProfile.userType.type == 'FLIGHT_DEPT'){ authService.setUser(result.data); $state.go('app.flightDepDashboard') |
4bb02bb84
|
55 56 57 58 59 60 |
}else{ localStorage.clear(); toastr.info("Please check username and password"); } deferred.resolve(result.data); },function (result){ |
67d43a8c6
|
61 |
|
4bb02bb84
|
62 63 64 65 |
deferred.resolve(result.data); }); return deferred.promise; } |
dd378d69f
|
66 67 68 |
authService.setAuth = function(data) { localStorage.setItem('loginStatus', data); } |
4bb02bb84
|
69 70 |
authService.logout = function(data) { |
dd378d69f
|
71 72 73 |
localStorage.clear(); $rootScope.isAuthenticated = false; $rootScope.$broadcast(AUTH_EVENTS.updateUser); |
4bb02bb84
|
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
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
![]() |
93 |
|
4bb02bb84
|
94 |
authService.setUser = function (authData) { |
67d43a8c6
|
95 |
//console.log('authdata', authData); |
4bb02bb84
|
96 |
window.localStorage.setItem("currentUser", JSON.stringify(authData.userProfile.userType)); |
4bda5699a
![]() |
97 |
window.localStorage.setItem("userProfileId", JSON.stringify(authData.userProfile.id)); |
8f7dbe97c
![]() |
98 |
window.localStorage.setItem('currentUserName', JSON.stringify(authData.userProfile.firstName +' '+ authData.userProfile.lastName)); |
8041a48e1
|
99 |
window.localStorage.setItem('companyName', JSON.stringify(authData.userProfile.companyName)); |
4bb02bb84
|
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
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; } })(); |