Created
July 6, 2014 15:27
-
-
Save iambigd/07b06de46aaea2512dea to your computer and use it in GitHub Desktop.
Angular AuthInterceptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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