auth.service.js
3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
95
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
124
125
126
127
128
129
(function() {
'use strict';
angular
.module('dialoga')
.factory('Session', Session)
.factory('AuthService', AuthService)
.factory('AuthInterceptor', AuthInterceptor);
/** @ngInject */
function AuthService($http, $rootScope, Session, AUTH_EVENTS, API, $log) {
function login (credentials) {
var hostProd = 'http://login.dialoga.gov.br';
var url = hostProd + '/api/v1/login';
var encodedData = 'login=' + credentials.username + '&password=' + credentials.password;
return $http
.post(url, encodedData)
.then(function(response) {
$log.debug('AuthService.login [SUCCESS] response', response);
var currentUser = Session.create(response.data);
$rootScope.$broadcast(AUTH_EVENTS.loginSuccess, currentUser);
return currentUser;
}, function(response) {
$log.debug('AuthService.login [FAIL] response', response);
$rootScope.$broadcast(AUTH_EVENTS.loginFailed);
});
}
function logout () {
Session.destroy();
$rootScope.$broadcast(AUTH_EVENTS.logoutSuccess);
}
function isAuthenticated () {
return !!Session.userId;
}
function isAuthorized (authorizedRoles) {
if (!angular.isArray(authorizedRoles)) {
authorizedRoles = [authorizedRoles];
}
return (service.isAuthenticated() && authorizedRoles.indexOf(Session.userRole) !== -1);
}
var service = {
login: login,
logout: logout,
isAuthenticated: isAuthenticated,
isAuthorized: isAuthorized
};
$log.debug('AuthService', service);
return service;
}
/** @ngInject */
function Session($localStorage, $log) {
var service = {};
// $localStorage.currentUser = $localStorage.currentUser || null;
service.create = function(data) {
$localStorage.currentUser = data;
$log.debug('User session created.', $localStorage.currentUser);
return $localStorage.currentUser;
};
service.destroy = function() {
delete $localStorage.currentUser;
$log.debug('User session destroyed.');
};
service.getCurrentUser = function () {
return $localStorage.currentUser;
};
return service;
}
/** @ngInject */
function AuthInterceptor ($rootScope, $q, AUTH_EVENTS) {
return {
responseError: function(response) {
$rootScope.$broadcast({
401: AUTH_EVENTS.notAuthenticated,
403: AUTH_EVENTS.notAuthorized,
419: AUTH_EVENTS.sessionTimeout,
440: AUTH_EVENTS.sessionTimeout
}[response.status], response);
return $q.reject(response);
}
};
}
// /** @ngInject */
// function AuthResolver($q, $rootScope, $state){
// return {
// resolve: function () {
// var deferred = $q.defer();
// var unwatch = $rootScope.$watch('currentUser', function (currentUser) {
// if (angular.isDefined(currentUser)) {
// if (currentUser) {
// deferred.resolve(currentUser);
// } else {
// deferred.reject();
// // TODO: too many responsibilities?
// $state.go('login');
// }
// unwatch();
// }
// });
// return deferred.promise;
// }
// };
// }
})();