Skip to content

Instantly share code, notes, and snippets.

@jkuchynka
Created April 17, 2014 19:26
Show Gist options
  • Save jkuchynka/11006322 to your computer and use it in GitHub Desktop.
Save jkuchynka/11006322 to your computer and use it in GitHub Desktop.
angular401interceptor
angular.module('angularAuth', ['ngCookies'])
.config([
'$routeProvider',
'$locationProvider',
'$httpProvider',
function ($routeProvider, $locationProvider, $httpProvider) {
// ...
var interceptor = ['$location', '$q', function($location, $q) {
function success(response) {
return response;
}
function error(response) {
if(response.status === 401) {
// Or login dialog
$location.path('/login');
return $q.reject(response);
}
else {
return $q.reject(response);
}
}
return function(promise) {
return promise.then(success, error);
}
}];
$httpProvider.responseInterceptors.push(interceptor);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment