|
| 1 | +/*global angular:true, browser:true */ |
| 2 | + |
1 | 3 | /**
|
2 | 4 | * @license HTTP Auth Interceptor Module for AngularJS
|
3 | 5 | * (c) 2012 Witold Szczerba
|
4 | 6 | * License: MIT
|
5 | 7 | */
|
6 | 8 | angular.module('http-auth-interceptor', [])
|
7 | 9 |
|
8 |
| - .provider('authService', function() { |
| 10 | + .provider('authService', function () { |
9 | 11 | /**
|
10 | 12 | * Holds all the requests which failed due to 401 response,
|
11 | 13 | * so they can be re-requested in future, once login is completed.
|
12 | 14 | */
|
13 | 15 | var buffer = [];
|
14 |
| - |
| 16 | + |
| 17 | + /** |
| 18 | + * Holds a list of functions that define rules for ignoring |
| 19 | + * the addition of requests to the buffer. |
| 20 | + */ |
| 21 | + var ignoreUrlExpressions = []; |
| 22 | + |
| 23 | + /** |
| 24 | + * Adds functions to the `ignoreUrlExpressions` array. |
| 25 | + * The fn function takes a URL as a response as an argument and returns |
| 26 | + * `true` (to ignore the URL) or `false` (to allow the URL). When `true` is |
| 27 | + * returned no other expressions will be tested. |
| 28 | + */ |
| 29 | + this.addIgnoreUrlExpression = function (fn) { |
| 30 | + if (angular.isFunction(fn)) { ignoreUrlExpressions.push(fn); } |
| 31 | + return this; |
| 32 | + }; |
| 33 | + |
| 34 | + /** |
| 35 | + * Executes each of the ignore expressions to determine whether the URL |
| 36 | + * should be ignored. |
| 37 | + * |
| 38 | + * Example: |
| 39 | + * |
| 40 | + * angular.module('mod', ['http-auth-interceptor']) |
| 41 | + * .config(function (authServiceProvider) { |
| 42 | + * authServiceProvider.addIgnoreUrlExpression(function (response) { |
| 43 | + * return response.config.url === "/api/auth"; |
| 44 | + * }); |
| 45 | + * }); |
| 46 | + */ |
| 47 | + this.shouldIgnoreUrl = function (response) { |
| 48 | + var fn, i, j = ignoreUrlExpressions.length; |
| 49 | + |
| 50 | + for (i = 0; i < j; i++) { |
| 51 | + fn = ignoreUrlExpressions[i]; |
| 52 | + if (fn(response) === true) { return true; } |
| 53 | + } |
| 54 | + |
| 55 | + return false; |
| 56 | + }; |
| 57 | + |
15 | 58 | /**
|
16 | 59 | * Required by HTTP interceptor.
|
17 | 60 | * Function is attached to provider to be invisible for regular users of this service.
|
18 | 61 | */
|
19 |
| - this.pushToBuffer = function(config, deferred) { |
| 62 | + this.pushToBuffer = function (config, deferred) { |
20 | 63 | buffer.push({
|
21 |
| - config: config, |
| 64 | + config: config, |
22 | 65 | deferred: deferred
|
23 | 66 | });
|
24 |
| - } |
25 |
| - |
26 |
| - this.$get = ['$rootScope','$injector', function($rootScope, $injector) { |
| 67 | + }; |
| 68 | + |
| 69 | + this.$get = ['$rootScope', '$injector', function ($rootScope, $injector) { |
27 | 70 | var $http; //initialized later because of circular dependency problem
|
28 | 71 | function retry(config, deferred) {
|
29 | 72 | $http = $http || $injector.get('$http');
|
30 |
| - $http(config).then(function(response) { |
| 73 | + $http(config).then(function (response) { |
31 | 74 | deferred.resolve(response);
|
32 | 75 | });
|
33 | 76 | }
|
34 | 77 | function retryAll() {
|
35 |
| - for (var i = 0; i < buffer.length; ++i) { |
| 78 | + var i; |
| 79 | + |
| 80 | + for (i = 0; i < buffer.length; ++i) { |
36 | 81 | retry(buffer[i].config, buffer[i].deferred);
|
37 | 82 | }
|
38 | 83 | buffer = [];
|
39 | 84 | }
|
40 | 85 |
|
41 | 86 | return {
|
42 |
| - loginConfirmed: function() { |
| 87 | + loginConfirmed: function () { |
43 | 88 | $rootScope.$broadcast('event:auth-loginConfirmed');
|
44 | 89 | retryAll();
|
45 | 90 | }
|
46 |
| - } |
47 |
| - }] |
| 91 | + }; |
| 92 | + }]; |
48 | 93 | })
|
49 | 94 |
|
50 | 95 | /**
|
51 | 96 | * $http interceptor.
|
52 | 97 | * On 401 response - it stores the request and broadcasts 'event:angular-auth-loginRequired'.
|
53 | 98 | */
|
54 |
| - .config(['$httpProvider', 'authServiceProvider', function($httpProvider, authServiceProvider) { |
55 |
| - |
56 |
| - var interceptor = ['$rootScope', '$q', function($rootScope, $q) { |
| 99 | + .config(['$httpProvider', 'authServiceProvider', function ($httpProvider, authServiceProvider) { |
| 100 | + |
| 101 | + var interceptor = ['$rootScope', '$q', function ($rootScope, $q) { |
57 | 102 | function success(response) {
|
58 | 103 | return response;
|
59 | 104 | }
|
60 |
| - |
| 105 | + |
61 | 106 | function error(response) {
|
62 | 107 | if (response.status === 401) {
|
63 | 108 | var deferred = $q.defer();
|
64 |
| - authServiceProvider.pushToBuffer(response.config, deferred); |
| 109 | + |
| 110 | + if (!authServiceProvider.shouldIgnoreUrl(response)) { |
| 111 | + authServiceProvider.pushToBuffer(response.config, deferred); |
| 112 | + } |
| 113 | + |
65 | 114 | $rootScope.$broadcast('event:auth-loginRequired');
|
66 | 115 | return deferred.promise;
|
67 | 116 | }
|
68 | 117 | // otherwise
|
69 | 118 | return $q.reject(response);
|
70 | 119 | }
|
71 |
| - |
72 |
| - return function(promise) { |
| 120 | + |
| 121 | + return function (promise) { |
73 | 122 | return promise.then(success, error);
|
74 |
| - } |
75 |
| - |
| 123 | + }; |
| 124 | + |
76 | 125 | }];
|
77 | 126 | $httpProvider.responseInterceptors.push(interceptor);
|
78 | 127 | }]);
|
0 commit comments