mirror of https://github.com/portainer/portainer
refactor(app): refactor unauthenticated state management (#2393)
* refactor(app): refactor Authentication service * refactor(app): refactor unauthenticated state managementpull/2396/head
parent
4f9a8180f9
commit
b5dfaff292
|
@ -37,9 +37,13 @@ function ($rootScope, $state, Authentication, authManager, StateManager, Endpoin
|
||||||
|
|
||||||
function initAuthentication(authManager, Authentication, $rootScope, $state) {
|
function initAuthentication(authManager, Authentication, $rootScope, $state) {
|
||||||
authManager.checkAuthOnRefresh();
|
authManager.checkAuthOnRefresh();
|
||||||
authManager.redirectWhenUnauthenticated();
|
|
||||||
Authentication.init();
|
Authentication.init();
|
||||||
$rootScope.$on('tokenHasExpired', function() {
|
|
||||||
|
// The unauthenticated event is broadcasted by the jwtInterceptor when
|
||||||
|
// hitting a 401. We're using this instead of the usual combination of
|
||||||
|
// authManager.redirectWhenUnauthenticated() + unauthenticatedRedirector
|
||||||
|
// to have more controls on which URL should trigger the unauthenticated state.
|
||||||
|
$rootScope.$on('unauthenticated', function () {
|
||||||
$state.go('portainer.auth', {error: 'Your session has expired'});
|
$state.go('portainer.auth', {error: 'Your session has expired'});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,9 +14,6 @@ angular.module('portainer')
|
||||||
jwtOptionsProvider.config({
|
jwtOptionsProvider.config({
|
||||||
tokenGetter: ['LocalStorage', function(LocalStorage) {
|
tokenGetter: ['LocalStorage', function(LocalStorage) {
|
||||||
return LocalStorage.getJWT();
|
return LocalStorage.getJWT();
|
||||||
}],
|
|
||||||
unauthenticatedRedirector: ['$state', function($state) {
|
|
||||||
$state.go('portainer.auth', {error: 'Your session has expired'});
|
|
||||||
}]
|
}]
|
||||||
});
|
});
|
||||||
$httpProvider.interceptors.push('jwtInterceptor');
|
$httpProvider.interceptors.push('jwtInterceptor');
|
||||||
|
|
|
@ -2,43 +2,59 @@ angular.module('portainer.app')
|
||||||
.factory('Authentication', ['$q', 'Auth', 'jwtHelper', 'LocalStorage', 'StateManager', 'EndpointProvider', function AuthenticationFactory($q, Auth, jwtHelper, LocalStorage, StateManager, EndpointProvider) {
|
.factory('Authentication', ['$q', 'Auth', 'jwtHelper', 'LocalStorage', 'StateManager', 'EndpointProvider', function AuthenticationFactory($q, Auth, jwtHelper, LocalStorage, StateManager, EndpointProvider) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
var service = {};
|
||||||
var user = {};
|
var user = {};
|
||||||
return {
|
|
||||||
init: function() {
|
service.init = init;
|
||||||
|
service.login = login;
|
||||||
|
service.logout = logout;
|
||||||
|
service.isAuthenticated = isAuthenticated;
|
||||||
|
service.getUserDetails = getUserDetails;
|
||||||
|
|
||||||
|
function init() {
|
||||||
var jwt = LocalStorage.getJWT();
|
var jwt = LocalStorage.getJWT();
|
||||||
|
|
||||||
if (jwt) {
|
if (jwt) {
|
||||||
var tokenPayload = jwtHelper.decodeToken(jwt);
|
var tokenPayload = jwtHelper.decodeToken(jwt);
|
||||||
user.username = tokenPayload.username;
|
user.username = tokenPayload.username;
|
||||||
user.ID = tokenPayload.id;
|
user.ID = tokenPayload.id;
|
||||||
user.role = tokenPayload.role;
|
user.role = tokenPayload.role;
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
login: function(username, password) {
|
|
||||||
return $q(function (resolve, reject) {
|
function login(username, password) {
|
||||||
|
var deferred = $q.defer();
|
||||||
|
|
||||||
Auth.login({username: username, password: password}).$promise
|
Auth.login({username: username, password: password}).$promise
|
||||||
.then(function(data) {
|
.then(function success(data) {
|
||||||
LocalStorage.storeJWT(data.jwt);
|
LocalStorage.storeJWT(data.jwt);
|
||||||
var tokenPayload = jwtHelper.decodeToken(data.jwt);
|
var tokenPayload = jwtHelper.decodeToken(data.jwt);
|
||||||
user.username = username;
|
user.username = username;
|
||||||
user.ID = tokenPayload.id;
|
user.ID = tokenPayload.id;
|
||||||
user.role = tokenPayload.role;
|
user.role = tokenPayload.role;
|
||||||
resolve();
|
deferred.resolve();
|
||||||
}, function() {
|
})
|
||||||
reject();
|
.catch(function error() {
|
||||||
|
deferred.reject();
|
||||||
});
|
});
|
||||||
});
|
|
||||||
},
|
return deferred.promise;
|
||||||
logout: function() {
|
}
|
||||||
|
|
||||||
|
function logout() {
|
||||||
StateManager.clean();
|
StateManager.clean();
|
||||||
EndpointProvider.clean();
|
EndpointProvider.clean();
|
||||||
LocalStorage.clean();
|
LocalStorage.clean();
|
||||||
},
|
}
|
||||||
isAuthenticated: function() {
|
|
||||||
|
function isAuthenticated() {
|
||||||
var jwt = LocalStorage.getJWT();
|
var jwt = LocalStorage.getJWT();
|
||||||
return jwt && !jwtHelper.isTokenExpired(jwt);
|
return jwt && !jwtHelper.isTokenExpired(jwt);
|
||||||
},
|
}
|
||||||
getUserDetails: function() {
|
|
||||||
|
function getUserDetails() {
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
return service;
|
||||||
}]);
|
}]);
|
||||||
|
|
Loading…
Reference in New Issue