Blame view

app/partials/login/login.service.js 5.55 KB
4bb02bb84   Rishav   new integration w...
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   Swarn Singh   fuel manager desi...
13
         // console.log(currentUserDetail)
4bb02bb84   Rishav   new integration w...
14
          if (currentUserDetail) {
671643c9f   Kuldeep Arora   additional user
15
              if(currentUserDetail.type == 'ADMIN' || currentUserDetail.type == 'FBO' || currentUserDetail.type == 'ADDITIONAL'){
4bb02bb84   Rishav   new integration w...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
                  $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   Kuldeep Arora   login page update
34
                deferred.resolve(result);
4bb02bb84   Rishav   new integration w...
35
            },function (result){
67d43a8c6   Kuldeep Arora   login page update
36
                deferred.resolve(result);
4bb02bb84   Rishav   new integration w...
37
38
39
40
41
42
43
44
45
46
47
            })
            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){
bfbbd8668   Kuldeep Arora   UI service
48
                  console.log("kd",result.data.userProfile.userType.type)
671643c9f   Kuldeep Arora   additional user
49
                  if(result.data.userProfile.userType.type == 'ADMIN' || result.data.userProfile.userType.type == 'FBO'  || result.data.userProfile.userType.type == 'ADDITIONAL'){
bfbbd8668   Kuldeep Arora   UI service
50
                    console.log("usertype",result.data.userProfile.userType.type)
4bb02bb84   Rishav   new integration w...
51
52
                    authService.setUser(result.data);
                    $state.go('app.dashboard')
671643c9f   Kuldeep Arora   additional user
53
                  }else if(result.data.userProfile.userType.type == 'FLIGHT_DEPT' ){
c277275bb   Mr. Hot Foods   login changes
54
55
                      authService.setUser(result.data);
                      $state.go('app.flightDepDashboard')
4bb02bb84   Rishav   new integration w...
56
57
58
59
60
61
                  }else{
                    localStorage.clear();
                    toastr.info("Please check username and password");
                  }
                  deferred.resolve(result.data);
              },function (result){
67d43a8c6   Kuldeep Arora   login page update
62

4bb02bb84   Rishav   new integration w...
63
64
65
66
                  deferred.resolve(result.data);
              });
              return deferred.promise;
          }
dd378d69f   Mr. Hot Foods   changes in flight...
67
68
69
          authService.setAuth = function(data) {
              localStorage.setItem('loginStatus', data);
          }
4bb02bb84   Rishav   new integration w...
70
71
  
          authService.logout = function(data) {
dd378d69f   Mr. Hot Foods   changes in flight...
72
73
74
            localStorage.clear();
            $rootScope.isAuthenticated = false;
            $rootScope.$broadcast(AUTH_EVENTS.updateUser);
4bb02bb84   Rishav   new integration w...
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  
            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;
          }
          
f62f8940f   Kuldeep Arora   UI changes
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  
          authService.getAdditionalAccess = function(email) {
            var deferred = $q.defer();
            $http({
                method : 'GET',
                url : BASE_URL.url +'/additionalAccount/'+email,
                headers : {'Content-Type': 'application/json'},
            })
            .then(function (result){
                deferred.resolve(result.data);
            },function (result){
                deferred.resolve(result.data);
            })
            return deferred.promise;
          }
4bb02bb84   Rishav   new integration w...
104
105
106
107
108
        
  
          authService.getUser = function () {
            return JSON.parse(window.localStorage.getItem("currentUser"));
          }
8f7dbe97c   Swarn Singh   fuel order comple...
109
          
4bb02bb84   Rishav   new integration w...
110
          authService.setUser = function (authData) {
67d43a8c6   Kuldeep Arora   login page update
111
              //console.log('authdata', authData);
4bb02bb84   Rishav   new integration w...
112
              window.localStorage.setItem("currentUser", JSON.stringify(authData.userProfile.userType));
4bda5699a   Swarn Singh   fuel manager desi...
113
              window.localStorage.setItem("userProfileId", JSON.stringify(authData.userProfile.id));
8f7dbe97c   Swarn Singh   fuel order comple...
114
              window.localStorage.setItem('currentUserName', JSON.stringify(authData.userProfile.firstName +' '+ authData.userProfile.lastName));
8041a48e1   Kuldeep Arora   Editable companyn...
115
              window.localStorage.setItem('companyName', JSON.stringify(authData.userProfile.companyName));
671643c9f   Kuldeep Arora   additional user
116
              if(authData.userProfile.userType.type == 'ADMIN' || authData.userProfile.userType.type == 'FBO' || authData.userProfile.userType.type == 'ADDITIONAL'){
bfbbd8668   Kuldeep Arora   UI service
117
                 console.log("setUser",authData.userProfile.userType.type)
4bb02bb84   Rishav   new integration w...
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
                  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;
      }
        
  })();