|
7 | 7 | */
|
8 | 8 | (function () {
|
9 | 9 | 'use strict';
|
10 |
| - |
| 10 | + |
11 | 11 | angular.module('http-auth-interceptor', ['http-auth-interceptor-buffer'])
|
12 | 12 |
|
13 | 13 | .factory('authService', ['$rootScope','httpBuffer', function($rootScope, httpBuffer) {
|
14 | 14 | return {
|
15 | 15 | /**
|
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 |
17 | 17 | * retry of all deferred requests.
|
18 | 18 | * @param data an optional argument to pass on to $broadcast which may be useful for
|
19 | 19 | * example if you need to pass through details of the user that was logged in
|
20 | 20 | */
|
21 |
| - loginConfirmed: function(data) { |
| 21 | + loginConfirmed: function(data, configUpdater) { |
| 22 | + var updater = configUpdater || function(config) {return config;}; |
22 | 23 | $rootScope.$broadcast('event:auth-loginConfirmed', data);
|
23 |
| - httpBuffer.retryAll(); |
| 24 | + httpBuffer.retryAll(updater); |
24 | 25 | }
|
25 | 26 | };
|
26 | 27 | }])
|
27 | 28 |
|
28 | 29 | /**
|
29 | 30 | * $http interceptor.
|
30 |
| - * On 401 response (without 'ignoreAuthModule' option) stores the request |
| 31 | + * On 401 response (without 'ignoreAuthModule' option) stores the request |
31 | 32 | * and broadcasts 'event:angular-auth-loginRequired'.
|
32 | 33 | */
|
33 | 34 | .config(['$httpProvider', function($httpProvider) {
|
34 |
| - |
| 35 | + |
35 | 36 | var interceptor = ['$rootScope', '$q', 'httpBuffer', function($rootScope, $q, httpBuffer) {
|
36 | 37 | function success(response) {
|
37 | 38 | return response;
|
38 | 39 | }
|
39 |
| - |
| 40 | + |
40 | 41 | function error(response) {
|
41 | 42 | if (response.status === 401 && !response.config.ignoreAuthModule) {
|
42 | 43 | var deferred = $q.defer();
|
|
47 | 48 | // otherwise, default behaviour
|
48 | 49 | return $q.reject(response);
|
49 | 50 | }
|
50 |
| - |
| 51 | + |
51 | 52 | return function(promise) {
|
52 | 53 | return promise.then(success, error);
|
53 | 54 | };
|
54 |
| - |
| 55 | + |
55 | 56 | }];
|
56 | 57 | $httpProvider.responseInterceptors.push(interceptor);
|
57 | 58 | }]);
|
58 |
| - |
| 59 | + |
59 | 60 | /**
|
60 | 61 | * Private module, a utility, required internally by 'http-auth-interceptor'.
|
61 | 62 | */
|
|
64 | 65 | .factory('httpBuffer', ['$injector', function($injector) {
|
65 | 66 | /** Holds all the requests, so they can be re-requested in future. */
|
66 | 67 | var buffer = [];
|
67 |
| - |
| 68 | + |
68 | 69 | /** Service initialized later because of circular dependency problem. */
|
69 |
| - var $http; |
70 |
| - |
| 70 | + var $http; |
| 71 | + |
71 | 72 | function retryHttpRequest(config, deferred) {
|
72 | 73 | function successCallback(response) {
|
73 | 74 | deferred.resolve(response);
|
|
78 | 79 | $http = $http || $injector.get('$http');
|
79 | 80 | $http(config).then(successCallback, errorCallback);
|
80 | 81 | }
|
81 |
| - |
| 82 | + |
82 | 83 | return {
|
83 | 84 | /**
|
84 | 85 | * Appends HTTP request configuration object with deferred response attached to buffer.
|
85 | 86 | */
|
86 | 87 | append: function(config, deferred) {
|
87 | 88 | buffer.push({
|
88 |
| - config: config, |
| 89 | + config: config, |
89 | 90 | deferred: deferred
|
90 |
| - }); |
| 91 | + }); |
91 | 92 | },
|
92 |
| - |
| 93 | + |
93 | 94 | /**
|
94 | 95 | * Retries all the buffered requests clears the buffer.
|
95 | 96 | */
|
96 |
| - retryAll: function() { |
| 97 | + retryAll: function(updater) { |
97 | 98 | 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); |
99 | 100 | }
|
100 | 101 | buffer = [];
|
101 | 102 | }
|
|
0 commit comments