7
7
*/
8
8
( function ( ) {
9
9
'use strict' ;
10
- angular . module ( 'http-auth-interceptor' , [ ] )
10
+
11
+ angular . module ( 'http-auth-interceptor' , [ 'http-auth-interceptor-buffer' ] )
11
12
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 ( ) ;
43
18
}
44
-
45
- return {
46
- loginConfirmed : function ( ) {
47
- $rootScope . $broadcast ( 'event:auth-loginConfirmed' ) ;
48
- retryAll ( ) ;
49
- }
50
- }
51
- } ]
52
- } )
19
+ } ;
20
+ } ] )
53
21
54
22
/**
55
23
* $http interceptor.
56
- * On 401 response (without 'ignoreAuthModule' option) - it stores the request
24
+ * On 401 response (without 'ignoreAuthModule' option) stores the request
57
25
* and broadcasts 'event:angular-auth-loginRequired'.
58
26
*/
59
- . config ( [ '$httpProvider' , 'authServiceProvider' , function ( $httpProvider , authServiceProvider ) {
27
+ . config ( [ '$httpProvider' , function ( $httpProvider ) {
60
28
61
- var interceptor = [ '$rootScope' , '$q' , function ( $rootScope , $q ) {
29
+ var interceptor = [ '$rootScope' , '$q' , 'httpBuffer' , function ( $rootScope , $q , httpBuffer ) {
62
30
function success ( response ) {
63
31
return response ;
64
32
}
65
33
66
34
function error ( response ) {
67
35
if ( response . status === 401 && ! response . config . ignoreAuthModule ) {
68
36
var deferred = $q . defer ( ) ;
69
- authServiceProvider . pushToBuffer ( response . config , deferred ) ;
37
+ httpBuffer . append ( response . config , deferred ) ;
70
38
$rootScope . $broadcast ( 'event:auth-loginRequired' ) ;
71
39
return deferred . promise ;
72
40
}
76
44
77
45
return function ( promise ) {
78
46
return promise . then ( success , error ) ;
79
- }
47
+ } ;
80
48
81
49
} ] ;
82
50
$httpProvider . responseInterceptors . push ( interceptor ) ;
83
51
} ] ) ;
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
+ } ] ) ;
84
94
} ) ( ) ;
0 commit comments