Created
September 23, 2014 16:45
-
-
Save cerkit/2639bd7db12e3ba2fa4e to your computer and use it in GitHub Desktop.
AngularJS Client App for basic authentication - Config
This file contains hidden or 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
/** | |
* $http interceptor. | |
* On 401 response - it stores the request and broadcasts 'event:auth-loginRequired'. | |
*/ | |
angular.module('app').config(function ($httpProvider) { | |
$httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; | |
var interceptor = ['$rootScope', '$q', function (scope, $q) { | |
function success(response) { | |
return response; | |
} | |
function error(response) { | |
var status = response.status; | |
if (status === 401) { | |
var deferred = $q.defer(); | |
var req = { | |
config: response.config, | |
deferred: deferred | |
}; | |
//scope.requests401.push(req); | |
scope.$broadcast('event:auth-loginRequired'); | |
return deferred.promise; | |
} | |
// 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