Blame view

app/partials/login/login.service.js 4.36 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
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   Swarn Singh   fuel manager desi...
37
              //console.log(result.data)
4bb02bb84   Rishav   new integration w...
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
                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;
          }
  
          // authService.setAuth = function(data) {
          //     localStorage.setItem('loginStatus', data);
          // }
  
          authService.logout = function(data) {
  
            // localStorage.clear();
            // $rootScope.isAuthenticated = false;
            // $rootScope.$broadcast(AUTH_EVENTS.updateUser);
  
            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"));
          }
        
          authService.setUser = function (authData) {
              window.localStorage.setItem("currentUser", JSON.stringify(authData.userProfile.userType));
4bda5699a   Swarn Singh   fuel manager desi...
95
              window.localStorage.setItem("userProfileId", JSON.stringify(authData.userProfile.id));
4bb02bb84   Rishav   new integration w...
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;
      }
        
  })();