Skip to content

Commit

Permalink
Issue witoldsz#57: Provide confirmLogin and cancelLogin functions as …
Browse files Browse the repository at this point in the history
…an argument to the 'event:auth-loginRequired' broadcast
  • Loading branch information
christianrondeau committed Jun 4, 2014
1 parent 3c02454 commit 61ba732
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/http-auth-interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,47 @@

angular.module('http-auth-interceptor', ['http-auth-interceptor-buffer'])

.factory('authService', ['$rootScope','httpBuffer', function($rootScope, httpBuffer) {
return {
/**
* $http interceptor.
* On 401 response (without 'ignoreAuthModule' option) stores the request
* and broadcasts 'event:angular-auth-loginRequired'.
*/
.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push(['$rootScope', '$q', 'httpBuffer', function($rootScope, $q, httpBuffer) {

/**
* Call this function to indicate that authentication was successfull and trigger a
* retry of all deferred requests.
* @param data an optional argument to pass on to $broadcast which may be useful for
* example if you need to pass through details of the user that was logged in
*/
loginConfirmed: function(data, configUpdater) {
function loginConfirmed(data, configUpdater) {
var updater = configUpdater || function(config) {return config;};
$rootScope.$broadcast('event:auth-loginConfirmed', data);
httpBuffer.retryAll(updater);
},
}

/**
* Call this function to indicate that authentication should not proceed.
* All deferred requests will be abandoned or rejected (if reason is provided).
* @param data an optional argument to pass on to $broadcast.
* @param reason if provided, the requests are rejected; abandoned otherwise.
*/
loginCancelled: function(data, reason) {
function cancelLogin(data, reason) {
httpBuffer.rejectAll(reason);
$rootScope.$broadcast('event:auth-loginCancelled', data);
}
};
}])

/**
* $http interceptor.
* On 401 response (without 'ignoreAuthModule' option) stores the request
* and broadcasts 'event:angular-auth-loginRequired'.
*/
.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push(['$rootScope', '$q', 'httpBuffer', function($rootScope, $q, httpBuffer) {
return {

return {
responseError: function(rejection) {
if (rejection.status === 401 && !rejection.config.ignoreAuthModule) {
var deferred = $q.defer();
httpBuffer.append(rejection.config, deferred);
$rootScope.$broadcast('event:auth-loginRequired', rejection);
$rootScope.$broadcast('event:auth-loginRequired', {
rejection: rejection,
confirmLogin: loginConfirmed,
cancelLogin: cancelLogin
});
return deferred.promise;
}
// otherwise, default behaviour
Expand Down

0 comments on commit 61ba732

Please sign in to comment.