Skip to content

Instantly share code, notes, and snippets.

@iambigd
Created July 6, 2014 15:27
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 iambigd/07b06de46aaea2512dea to your computer and use it in GitHub Desktop.
Save iambigd/07b06de46aaea2512dea to your computer and use it in GitHub Desktop.
Angular AuthInterceptor
.factory('AuthInterceptor', function($rootScope, $q, API_EVENTS, $log) {
return {
// On response success
response: function(response) {
// Return the response or promise.
return response || $q.when(response);
},
responseError: function(response) {
/*
401 Unauthorized — The user is not logged in
403 Forbidden — The user is logged in but isn’t allowed access
419 Authentication Timeout (non standard) — Session has expired
440 Login Timeout (Microsoft only) — Session has expired
*/
if (response.status === 401) {
$rootScope.$broadcast(API_EVENTS.notAuthenticated,
response);
}
if (response.status === 403) {
$rootScope.$broadcast(API_EVENTS.notAuthorized,
response);
}
if (response.status === 419 || response.status === 440) {
$rootScope.$broadcast(API_EVENTS.sessionTimeout,
response);
}
return $q.reject(response);
}
};
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment