GUAC-1161: Reorganize authenticationInterceptor to match service pattern used elsewhere.

This commit is contained in:
Michael Jumper
2015-04-20 15:45:16 -07:00
parent 1010552c69
commit 999be47b20

View File

@@ -20,28 +20,44 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
angular.module('index').factory('authenticationInterceptor', ['$location', '$q', angular.module('index').factory('authenticationInterceptor', ['$injector',
function authenticationInterceptor($location, $q) { function authenticationInterceptor($injector) {
return { // Required services
var $location = $injector.get('$location');
var $q = $injector.get('$q');
// Redirect users to login if authorization fails var service = {};
responseError: function handleErrorResponse(rejection) {
// Only redirect failed authentication requests /**
if ((rejection.status === 401 || rejection.status === 403) * Redirect users to login if authorization fails. This is called
&& rejection.config.url === 'api/tokens') { * automatically when this service is registered as an interceptor, as
* documented at:
*
* https://docs.angularjs.org/api/ng/service/$http#interceptors
*
* @param {HttpPromise} rejection
* The promise associated with the HTTP request that failed.
*
* @returns {Promise}
* A rejected promise containing the originally-rejected HttpPromise.
*/
service.responseError = function responseError(rejection) {
// Only redirect if not already on login page // Only redirect failed authentication requests
if ($location.path() !== '/login/') if ((rejection.status === 401 || rejection.status === 403)
$location.path('/login/'); && rejection.config.url === 'api/tokens') {
} // Only redirect if not already on login page
if ($location.path() !== '/login/')
return $q.reject(rejection); $location.path('/login/');
} }
return $q.reject(rejection);
}; };
return service;
}]); }]);