2015-02-06 16:46:55 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('markdownFormatWdiffApp', [
|
|
|
|
'ngCookies',
|
|
|
|
'ngResource',
|
|
|
|
'ngSanitize',
|
|
|
|
'ngRoute',
|
|
|
|
'ui.bootstrap',
|
|
|
|
'btford.markdown'
|
|
|
|
])
|
|
|
|
.config(function ($routeProvider, $locationProvider, $httpProvider) {
|
|
|
|
$routeProvider
|
2015-04-12 06:37:38 +02:00
|
|
|
.otherwise ({
|
2015-02-06 16:46:55 +01:00
|
|
|
redirectTo: '/'
|
|
|
|
});
|
|
|
|
|
|
|
|
$locationProvider.html5Mode(true);
|
|
|
|
$httpProvider.interceptors.push('authInterceptor');
|
|
|
|
})
|
|
|
|
|
|
|
|
.factory('authInterceptor', function ($rootScope, $q, $cookieStore, $location) {
|
|
|
|
return {
|
|
|
|
// Add authorization token to headers
|
|
|
|
request: function (config) {
|
|
|
|
config.headers = config.headers || {};
|
|
|
|
if ($cookieStore.get('token')) {
|
|
|
|
config.headers.Authorization = 'Bearer ' + $cookieStore.get('token');
|
|
|
|
}
|
|
|
|
return config;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Intercept 401s and redirect you to login
|
|
|
|
responseError: function(response) {
|
|
|
|
if(response.status === 401) {
|
|
|
|
$location.path('/login');
|
|
|
|
// remove any stale tokens
|
|
|
|
$cookieStore.remove('token');
|
|
|
|
return $q.reject(response);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $q.reject(response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})
|
|
|
|
|
|
|
|
.run(function ($rootScope, $location, Auth) {
|
|
|
|
// Redirect to login if route requires auth and you're not logged in
|
|
|
|
$rootScope.$on('$routeChangeStart', function (event, next) {
|
|
|
|
Auth.isLoggedInAsync(function(loggedIn) {
|
|
|
|
if (next.authenticate && !loggedIn) {
|
|
|
|
$location.path('/login');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2015-04-12 06:37:38 +02:00
|
|
|
});
|