77 */
88( function ( ) {
99 'use strict' ;
10- angular . module ( 'http-auth-interceptor' , [ ] )
10+
11+ angular . module ( 'http-auth-interceptor' , [ 'http-auth-interceptor-buffer' ] )
1112
12- . provider ( 'authService' , function ( ) {
13- /**
14- * Holds all the requests which failed due to 401 response,
15- * so they can be re-requested in future, once login is completed.
16- */
17- var buffer = [ ] ;
18-
19- /**
20- * Required by HTTP interceptor.
21- * Function is attached to provider to be invisible for regular users of this service.
22- */
23- this . pushToBuffer = function ( config , deferred ) {
24- buffer . push ( {
25- config : config ,
26- deferred : deferred
27- } ) ;
28- } ;
29-
30- this . $get = [ '$rootScope' , '$injector' , function ( $rootScope , $injector ) {
31- var $http ; //initialized later because of circular dependency problem
32- function retry ( config , deferred ) {
33- $http = $http || $injector . get ( '$http' ) ;
34- $http ( config ) . then ( function ( response ) {
35- deferred . resolve ( response ) ;
36- } ) ;
37- }
38- function retryAll ( ) {
39- for ( var i = 0 ; i < buffer . length ; ++ i ) {
40- retry ( buffer [ i ] . config , buffer [ i ] . deferred ) ;
41- }
42- buffer = [ ] ;
13+ . factory ( 'authService' , [ '$rootScope' , 'httpBuffer' , function ( $rootScope , httpBuffer ) {
14+ return {
15+ loginConfirmed : function ( ) {
16+ $rootScope . $broadcast ( 'event:auth-loginConfirmed' ) ;
17+ httpBuffer . retryAll ( ) ;
4318 }
44-
45- return {
46- loginConfirmed : function ( ) {
47- $rootScope . $broadcast ( 'event:auth-loginConfirmed' ) ;
48- retryAll ( ) ;
49- }
50- }
51- } ]
52- } )
19+ } ;
20+ } ] )
5321
5422 /**
5523 * $http interceptor.
56- * On 401 response (without 'ignoreAuthModule' option) - it stores the request
24+ * On 401 response (without 'ignoreAuthModule' option) stores the request
5725 * and broadcasts 'event:angular-auth-loginRequired'.
5826 */
59- . config ( [ '$httpProvider' , 'authServiceProvider' , function ( $httpProvider , authServiceProvider ) {
27+ . config ( [ '$httpProvider' , function ( $httpProvider ) {
6028
61- var interceptor = [ '$rootScope' , '$q' , function ( $rootScope , $q ) {
29+ var interceptor = [ '$rootScope' , '$q' , 'httpBuffer' , function ( $rootScope , $q , httpBuffer ) {
6230 function success ( response ) {
6331 return response ;
6432 }
6533
6634 function error ( response ) {
6735 if ( response . status === 401 && ! response . config . ignoreAuthModule ) {
6836 var deferred = $q . defer ( ) ;
69- authServiceProvider . pushToBuffer ( response . config , deferred ) ;
37+ httpBuffer . append ( response . config , deferred ) ;
7038 $rootScope . $broadcast ( 'event:auth-loginRequired' ) ;
7139 return deferred . promise ;
7240 }
7644
7745 return function ( promise ) {
7846 return promise . then ( success , error ) ;
79- }
47+ } ;
8048
8149 } ] ;
8250 $httpProvider . responseInterceptors . push ( interceptor ) ;
8351 } ] ) ;
52+
53+ /**
54+ * Private module, an utility, required internally by 'http-auth-interceptor'.
55+ */
56+ angular . module ( 'http-auth-interceptor-buffer' , [ ] )
57+
58+ . factory ( 'httpBuffer' , [ '$injector' , function ( $injector ) {
59+ /** Holds all the requests, so they can be re-requested in future. */
60+ var buffer = [ ] ;
61+
62+ /** Service initialized later because of circular dependency problem. */
63+ var $http ;
64+
65+ function retryHttpRequest ( config , deferred ) {
66+ $http = $http || $injector . get ( '$http' ) ;
67+ $http ( config ) . then ( function ( response ) {
68+ deferred . resolve ( response ) ;
69+ } ) ;
70+ }
71+
72+ return {
73+ /**
74+ * Appends HTTP request configuration object with deferred response attached to buffer.
75+ */
76+ append : function ( config , deferred ) {
77+ buffer . push ( {
78+ config : config ,
79+ deferred : deferred
80+ } ) ;
81+ } ,
82+
83+ /**
84+ * Retries all the buffered requests clears the buffer.
85+ */
86+ retryAll : function ( ) {
87+ for ( var i = 0 ; i < buffer . length ; ++ i ) {
88+ retryHttpRequest ( buffer [ i ] . config , buffer [ i ] . deferred ) ;
89+ }
90+ buffer = [ ] ;
91+ }
92+ } ;
93+ } ] ) ;
8494} ) ( ) ;
0 commit comments