diff --git a/modules/core/client/app/init.js b/modules/core/client/app/init.js index b1c1ce08..f7580c76 100644 --- a/modules/core/client/app/init.js +++ b/modules/core/client/app/init.js @@ -4,13 +4,16 @@ angular.module(ApplicationConfiguration.applicationModuleName, ApplicationConfiguration.applicationModuleVendorDependencies); // Setting HTML5 Location Mode -angular.module(ApplicationConfiguration.applicationModuleName).config(['$locationProvider', - function ($locationProvider) { +angular.module(ApplicationConfiguration.applicationModuleName).config(['$locationProvider', '$httpProvider', + function ($locationProvider, $httpProvider) { $locationProvider.html5Mode(true).hashPrefix('!'); + + $httpProvider.interceptors.push('authInterceptor'); } ]); angular.module(ApplicationConfiguration.applicationModuleName).run(function ($rootScope, $state, Authentication) { + // Check authentication before changing state $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) { if (toState.data && toState.data.roles && toState.data.roles.length > 0) { @@ -24,11 +27,11 @@ angular.module(ApplicationConfiguration.applicationModuleName).run(function ($ro if (!allowed) { event.preventDefault(); - $state.go('authentication.signin', {}, { - notify: false - }).then(function () { - $rootScope.$broadcast('$stateChangeSuccess', 'authentication.signin', {}, toState, toParams); - }); + if (Authentication.user !== undefined && typeof Authentication.user === 'object') { + $state.go('forbidden'); + } else { + $state.go('authentication.signin'); + } } } }); diff --git a/modules/core/client/config/core.client.routes.js b/modules/core/client/config/core.client.routes.js index ee9cc4dc..cec96327 100644 --- a/modules/core/client/config/core.client.routes.js +++ b/modules/core/client/config/core.client.routes.js @@ -13,16 +13,30 @@ angular.module('core').config(['$stateProvider', '$urlRouterProvider', // Home state routing $stateProvider - .state('home', { - url: '/', - templateUrl: 'modules/core/client/views/home.client.view.html' - }) - .state('not-found', { - url: '/not-found', - templateUrl: 'modules/core/client/views/404.client.view.html', - data: { - ignoreState: true - } - }); + .state('home', { + url: '/', + templateUrl: 'modules/core/client/views/home.client.view.html' + }) + .state('not-found', { + url: '/not-found', + templateUrl: 'modules/core/client/views/404.client.view.html', + data: { + ignoreState: true + } + }) + .state('bad-request', { + url: '/bad-request', + templateUrl: 'modules/core/client/views/400.client.view.html', + data: { + ignoreState: true + } + }) + .state('forbidden', { + url: '/forbidden', + templateUrl: 'modules/core/client/views/403.client.view.html', + data: { + ignoreState: true + } + }); } ]); diff --git a/modules/core/client/services/interceptors/auth.interceptor.client.service.js b/modules/core/client/services/interceptors/auth.interceptor.client.service.js new file mode 100644 index 00000000..d8eb8b93 --- /dev/null +++ b/modules/core/client/services/interceptors/auth.interceptor.client.service.js @@ -0,0 +1,22 @@ +'use strict'; + +angular.module('core').factory('authInterceptor', ['$q', '$injector', + function ($q, $injector) { + return { + responseError: function(rejection) { + if (!rejection.config.ignoreAuthModule) { + switch (rejection.status) { + case 401: + $injector.get('$state').transitionTo('authentication.signin'); + break; + case 403: + $injector.get('$state').transitionTo('forbidden'); + break; + } + } + // otherwise, default behaviour + return $q.reject(rejection); + } + }; + } +]); diff --git a/modules/core/client/views/400.client.view.html b/modules/core/client/views/400.client.view.html new file mode 100644 index 00000000..efc28045 --- /dev/null +++ b/modules/core/client/views/400.client.view.html @@ -0,0 +1,6 @@ +