diff --git a/app/portainer/__module.js b/app/portainer/__module.js index 458b8818d..f615d488e 100644 --- a/app/portainer/__module.js +++ b/app/portainer/__module.js @@ -7,7 +7,7 @@ async function initAuthentication(authManager, Authentication, $rootScope, $stat // authManager.redirectWhenUnauthenticated() + unauthenticatedRedirector // to have more controls on which URL should trigger the unauthenticated state. $rootScope.$on('unauthenticated', function (event, data) { - if (!_.includes(data.config.url, '/v2/') && !_.includes(data.config.url, '/api/v4/')) { + if (!_.includes(data.config.url, '/v2/') && !_.includes(data.config.url, '/api/v4/') && isTransitionRequiresAuthentication($state.transition)) { $state.go('portainer.logout', { error: 'Your session has expired' }); } }); @@ -32,9 +32,8 @@ angular.module('portainer.app', ['portainer.oauth']).config([ try { const loggedIn = await initAuthentication(authManager, Authentication, $rootScope, $state); await StateManager.initialize(); - const nextTransition = $state.transition.to(); - if (!loggedIn && !['portainer.logout', 'portainer.auth', 'portainer.init'].some((route) => nextTransition.name.startsWith(route))) { - $state.go('portainer.auth'); + if (!loggedIn && isTransitionRequiresAuthentication($state.transition)) { + $state.go('portainer.logout'); return Promise.reject('Unauthenticated'); } } catch (err) { @@ -425,3 +424,13 @@ angular.module('portainer.app', ['portainer.oauth']).config([ $stateRegistryProvider.register(team); }, ]); + +function isTransitionRequiresAuthentication(transition) { + const UNAUTHENTICATED_ROUTES = ['portainer.logout', 'portainer.auth']; + if (!transition) { + return true; + } + const nextTransition = transition && transition.to(); + const nextTransitionName = nextTransition ? nextTransition.name : ''; + return !UNAUTHENTICATED_ROUTES.some((route) => nextTransitionName.startsWith(route)); +} diff --git a/app/portainer/services/stateManager.js b/app/portainer/services/stateManager.js index 70520cb7d..cf988e5ee 100644 --- a/app/portainer/services/stateManager.js +++ b/app/portainer/services/stateManager.js @@ -59,6 +59,7 @@ angular.module('portainer.app').factory('StateManager', [ manager.clean = function () { state.endpoint = {}; + state.application = {}; }; manager.updateLogo = function (logoURL) { diff --git a/app/portainer/views/auth/authController.js b/app/portainer/views/auth/authController.js index ff44d2669..b5f7a4b0f 100644 --- a/app/portainer/views/auth/authController.js +++ b/app/portainer/views/auth/authController.js @@ -147,6 +147,7 @@ class AuthenticationController { } async postLoginSteps() { + await this.StateManager.initialize(); await this.checkForEndpointsAsync(); await this.checkForLatestVersionAsync(); } diff --git a/app/portainer/views/init/admin/initAdminController.js b/app/portainer/views/init/admin/initAdminController.js index 65ccb4bb5..63bd27d02 100644 --- a/app/portainer/views/init/admin/initAdminController.js +++ b/app/portainer/views/init/admin/initAdminController.js @@ -32,9 +32,11 @@ angular.module('portainer.app').controller('InitAdminController', [ return Authentication.login(username, password); }) .then(function success() { - StateManager.updateEnableTelemetry($scope.formValues.enableTelemetry); return SettingsService.update({ enableTelemetry: $scope.formValues.enableTelemetry }); }) + .then(() => { + return StateManager.initialize(); + }) .then(function () { return EndpointService.endpoints(0, 100); })