Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b5d0cf1

Browse files
committed
Merge pull request witoldsz#36 from scollinson/master
Add function to update retried request configs
2 parents a643504 + 10dd81a commit b5d0cf1

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

src/http-auth-interceptor.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,37 @@
77
*/
88
(function () {
99
'use strict';
10-
10+
1111
angular.module('http-auth-interceptor', ['http-auth-interceptor-buffer'])
1212

1313
.factory('authService', ['$rootScope','httpBuffer', function($rootScope, httpBuffer) {
1414
return {
1515
/**
16-
* call this function to indicate that authentication was successfull and trigger a
16+
* call this function to indicate that authentication was successfull and trigger a
1717
* retry of all deferred requests.
1818
* @param data an optional argument to pass on to $broadcast which may be useful for
1919
* example if you need to pass through details of the user that was logged in
2020
*/
21-
loginConfirmed: function(data) {
21+
loginConfirmed: function(data, configUpdater) {
22+
var updater = configUpdater || function(config) {return config;};
2223
$rootScope.$broadcast('event:auth-loginConfirmed', data);
23-
httpBuffer.retryAll();
24+
httpBuffer.retryAll(updater);
2425
}
2526
};
2627
}])
2728

2829
/**
2930
* $http interceptor.
30-
* On 401 response (without 'ignoreAuthModule' option) stores the request
31+
* On 401 response (without 'ignoreAuthModule' option) stores the request
3132
* and broadcasts 'event:angular-auth-loginRequired'.
3233
*/
3334
.config(['$httpProvider', function($httpProvider) {
34-
35+
3536
var interceptor = ['$rootScope', '$q', 'httpBuffer', function($rootScope, $q, httpBuffer) {
3637
function success(response) {
3738
return response;
3839
}
39-
40+
4041
function error(response) {
4142
if (response.status === 401 && !response.config.ignoreAuthModule) {
4243
var deferred = $q.defer();
@@ -47,15 +48,15 @@
4748
// otherwise, default behaviour
4849
return $q.reject(response);
4950
}
50-
51+
5152
return function(promise) {
5253
return promise.then(success, error);
5354
};
54-
55+
5556
}];
5657
$httpProvider.responseInterceptors.push(interceptor);
5758
}]);
58-
59+
5960
/**
6061
* Private module, a utility, required internally by 'http-auth-interceptor'.
6162
*/
@@ -64,10 +65,10 @@
6465
.factory('httpBuffer', ['$injector', function($injector) {
6566
/** Holds all the requests, so they can be re-requested in future. */
6667
var buffer = [];
67-
68+
6869
/** Service initialized later because of circular dependency problem. */
69-
var $http;
70-
70+
var $http;
71+
7172
function retryHttpRequest(config, deferred) {
7273
function successCallback(response) {
7374
deferred.resolve(response);
@@ -78,24 +79,24 @@
7879
$http = $http || $injector.get('$http');
7980
$http(config).then(successCallback, errorCallback);
8081
}
81-
82+
8283
return {
8384
/**
8485
* Appends HTTP request configuration object with deferred response attached to buffer.
8586
*/
8687
append: function(config, deferred) {
8788
buffer.push({
88-
config: config,
89+
config: config,
8990
deferred: deferred
90-
});
91+
});
9192
},
92-
93+
9394
/**
9495
* Retries all the buffered requests clears the buffer.
9596
*/
96-
retryAll: function() {
97+
retryAll: function(updater) {
9798
for (var i = 0; i < buffer.length; ++i) {
98-
retryHttpRequest(buffer[i].config, buffer[i].deferred);
99+
retryHttpRequest(updater(buffer[i].config), buffer[i].deferred);
99100
}
100101
buffer = [];
101102
}

0 commit comments

Comments
 (0)