Skip to content

Instantly share code, notes, and snippets.

@xtbl
Last active January 3, 2016 11:39
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 xtbl/8457706 to your computer and use it in GitHub Desktop.
Save xtbl/8457706 to your computer and use it in GitHub Desktop.
AngularJS $interceptor example
// app.js
// http://djds4rce.wordpress.com/2013/08/13/understanding-angular-http-interceptors/
// http://blog.brunoscopelliti.com/http-response-interceptors
//any 404 error clean cookies and sends the user to login
var interceptor = ['$rootScope', '$q', '$location', '$cookieStore', function ($rootScope, $q, $location, $cookieStore) {
function success(response) {
return response;
}
function error(response) {
var status = response.status;
if (status == 401) {
$cookieStore.remove('user');
$rootScope.user = null;
return $location.path('/login');
}
// otherwise
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