Skip to content

Instantly share code, notes, and snippets.

@jianbo
Created April 30, 2014 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianbo/5139946b9860e8f11846 to your computer and use it in GitHub Desktop.
Save jianbo/5139946b9860e8f11846 to your computer and use it in GitHub Desktop.
interceptor
.config([
'$httpProvider', function($httpProvider, $injector) {
var interceptor;
interceptor = [
'$injector', '$rootScope', '$q', function($injector, $rootScope, $q) {
var error, success;
success = function(response) {
// console.log(response);
return response;
};
error = function(response) {
console.log(response.status);
if (response.status === 401) {
$rootScope.$broadcast('event:unauthorized');
$injector.invoke(function($http, $state) {
$state.go('authentication.login');
});
return response;
}
return $q.reject(response);
};
return function(promise) {
return promise.then(success, error);
};
}
];
return $httpProvider.responseInterceptors.push(interceptor);
}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment