Blame view

app/partials/login/login.service.js 4.74 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) {
4bda5699a   Swarn Singh   fuel manager desi...
15
              //console.log('aaya')
4bb02bb84   Rishav   new integration w...
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   Kuldeep Arora   login page update
35
                deferred.resolve(result);
4bb02bb84   Rishav   new integration w...
36
            },function (result){
67d43a8c6   Kuldeep Arora   login page update
37
                deferred.resolve(result);
4bb02bb84   Rishav   new integration w...
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){
67d43a8c6   Kuldeep Arora   login page update
49

c277275bb   Mr. Hot Foods   login changes
50
                  if(result.data.userProfile.userType.type == 'ADMIN' || result.data.userProfile.userType.type == 'FBO'){
4bb02bb84   Rishav   new integration w...
51
52
                    authService.setUser(result.data);
                    $state.go('app.dashboard')
c277275bb   Mr. Hot Foods   login changes
53
                  }else if(result.data.userProfile.userType.type == 'FLIGHT_DEPT'){
67d43a8c6   Kuldeep Arora   login page update
54
                      console.log("adsasd",result.data.userProfile)
c277275bb   Mr. Hot Foods   login changes
55
56
                      authService.setUser(result.data);
                      $state.go('app.flightDepDashboard')
4bb02bb84   Rishav   new integration w...
57
58
59
60
61
62
                  }else{
                    localStorage.clear();
                    toastr.info("Please check username and password");
                  }
                  deferred.resolve(result.data);
              },function (result){
67d43a8c6   Kuldeep Arora   login page update
63

4bb02bb84   Rishav   new integration w...
64
                  deferred.resolve(result.data);
67d43a8c6   Kuldeep Arora   login page update
65
                  console.log("--kd---",result.status)
4bb02bb84   Rishav   new integration w...
66
67
68
              });
              return deferred.promise;
          }
dd378d69f   Mr. Hot Foods   changes in flight...
69
70
71
          authService.setAuth = function(data) {
              localStorage.setItem('loginStatus', data);
          }
4bb02bb84   Rishav   new integration w...
72
73
  
          authService.logout = function(data) {
dd378d69f   Mr. Hot Foods   changes in flight...
74
75
76
            localStorage.clear();
            $rootScope.isAuthenticated = false;
            $rootScope.$broadcast(AUTH_EVENTS.updateUser);
4bb02bb84   Rishav   new integration w...
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  
            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   Swarn Singh   fuel order comple...
96
          
4bb02bb84   Rishav   new integration w...
97
          authService.setUser = function (authData) {
67d43a8c6   Kuldeep Arora   login page update
98
              //console.log('authdata', authData);
4bb02bb84   Rishav   new integration w...
99
              window.localStorage.setItem("currentUser", JSON.stringify(authData.userProfile.userType));
4bda5699a   Swarn Singh   fuel manager desi...
100
              window.localStorage.setItem("userProfileId", JSON.stringify(authData.userProfile.id));
8f7dbe97c   Swarn Singh   fuel order comple...
101
              window.localStorage.setItem('currentUserName', JSON.stringify(authData.userProfile.firstName +' '+ authData.userProfile.lastName));
4bb02bb84   Rishav   new integration w...
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
128
129
              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;
      }
        
  })();