JavaScript
- 9 ответов
- 0 вопросов
3
Вклад в тег
myApp.factory('AuthInterceptor', ['$window', '$q', '$log',
function($window, $q, $log) {
$log.debug('start AuthInterceptor');
return {
request: function(config) {
config.headers = config.headers || {};
if ($window.localStorage.getItem('key')) {
config.headers.Authorization = 'Token ' + $window.localStorage.getItem('key');
}
return config || $q.when(config);
},
response: function(response) {
if (response.status === 401) {
$log.debug('Hey! I"m javascript. Authorization is needed!');
}
return response || $q.when(response);
}
};
}
]);
myApp.config(['$locationProvider', '$stateProvider', '$urlRouterProvider', '$httpProvider',
function($locationProvider, $stateProvider, $urlRouterProvider, $httpProvider) {
// Authorization interceptor
$httpProvider.interceptors.push('AuthInterceptor');
}
]);