'use strict';
define([
'app/app',
'app/controllers/home',
'app/controllers/login',
'app/services/authentication',
'app/services/authorisation',
'app/services/session'
], function(app) {
app.run(['$rootScope', '$http' ,'$location', '$state', 'AuthenticationService', 'AuthorisationService', 'SessionService', function ($rootScope,
$http, $location, $state, authentication, authorisation, session) {
var NoAuthorisationRequiredRoute = ['/login'];
var AdminRoute = ['/admin'];
var routeClean = function(route) {
return _.find(NoAuthorisationRequiredRoute, function(noAuthRoute){
return _.str.startsWith(route, noAuthRoute);
});
}
var routeClean = function(route) {
return _.find(AdminRoute, function(noAuthRoute){
return _.str.startsWith(route, noAuthRoute);
});
}
$rootScope.$on('$stateChangeStart', function(event, toState, toStateParams, toParams, fromState, fromParams){
$rootScope.toState = toState;
$rootScope.toStateParams = toStateParams;
if(!routeClean($location.url()) && !authentication.isLoggedIn()) {
event.preventDefault();
$state.go('/login');
} else if(AdminRoute($location.url()) && !authorisation.validateRoleAdmin(session.currentUser)) {
event.preventDefault();
$state.go('/error')
}
});
$rootScope.$on('$stateChangeSuccess', function(currentRoute, previousRoute) {
$rootScope.title = $state.current.title;
});
$rootScope.$on('$stateNotFound',
function(event, unfoundState, fromState, fromParams){
console.log(unfoundState.to); // "lazy.state"
console.log(unfoundState.toParams); // {a:1, b:2}
console.log(unfoundState.options); // {inherit:false} + default options
});
}]);
app.config(['$locationProvider', '$stateProvider','$urlRouterProvider', '$httpProvider', function($locationProvider, $stateProvider, $urlRouterProvider, $httpProvider) {
$stateProvider.state('home', {
url: '/',
controller: 'HomeController',
templateUrl: '/assets/javascripts/app/partials/home.html',
title: 'Dashboard',
authenticate: true
});
$stateProvider.state('login', {
url: '/login',
controller: 'LoginController',
templateUrl: '/assets/javascripts/app/partials/login.html',
title: 'Login',
authenticate: false
});
$locationProvider.html5Mode(true);
$httpProvider.interceptors.push(function($q, $location) {
return {
'responseError': function(response) {
if(response.status === 401 || response.status === 403) {
$location.path('/login');
}
return $q.reject(response);
}
};
});
}]);
return app;
});
routes.js is