java-tutorials/spring-security-modules/spring-security-angular/client/anguarjs/app.js

40 lines
1.3 KiB
JavaScript
Raw Normal View History

(function () {
'use strict';
angular
.module('app', ['ngRoute'])
.config(config)
.run(run);
config.$inject = ['$routeProvider', '$locationProvider'];
function config($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
controller: 'HomeController',
templateUrl: 'home/home.view.html',
controllerAs: 'vm'
})
.when('/login', {
controller: 'LoginController',
templateUrl: 'login/login.view.html',
controllerAs: 'vm'
})
.otherwise({ redirectTo: '/login' });
}
run.$inject = ['$rootScope', '$location', '$http', '$window'];
function run($rootScope, $location, $http, $window) {
var userData = $window.sessionStorage.getItem('userData');
if (userData) {
$http.defaults.headers.common['Authorization'] = 'Basic ' + JSON.parse(userData).authData;
}
$rootScope.$on('$locationChangeStart', function (event, next, current) {
var restrictedPage = $.inArray($location.path(), ['/login']) === -1;
var loggedIn = $window.sessionStorage.getItem('userData');;
if (restrictedPage && !loggedIn) {
$location.path('/login');
}
});
}
})();