7
7
HttpErrorResponse ,
8
8
} from '@angular/common/http' ;
9
9
import { HTTP_INTERCEPTORS } from '@angular/common/http' ;
10
- import { BehaviorSubject , Observable , of , throwError , timer } from 'rxjs' ;
10
+ import { BehaviorSubject , Observable , from , of , throwError , timer } from 'rxjs' ;
11
11
import { AuthService } from '../services/v4/auth.service' ;
12
12
import { ResgridConfig } from '../resgrid-config' ;
13
13
import {
@@ -24,14 +24,9 @@ import { LoggerService } from '../services/logger.service';
24
24
export const retryCount = 3 ;
25
25
26
26
@Injectable ( {
27
- providedIn : 'root'
27
+ providedIn : 'root' ,
28
28
} )
29
29
export class HttpsRequestInterceptor implements HttpInterceptor {
30
- private isRefreshing = false ;
31
- private refreshTokenSubject : BehaviorSubject < any > = new BehaviorSubject < any > (
32
- null
33
- ) ;
34
-
35
30
constructor (
36
31
private authService : AuthService ,
37
32
private config : ResgridConfig ,
@@ -43,61 +38,43 @@ export class HttpsRequestInterceptor implements HttpInterceptor {
43
38
next : HttpHandler
44
39
) : Observable < HttpEvent < any > > {
45
40
if ( this . shouldAddTokenToRequest ( req . url ) ) {
46
- const dupReq = this . addAuthHeader ( req ) ;
47
-
48
- return next . handle ( dupReq ) . pipe (
49
- catchError ( ( error ) => {
50
- if ( error instanceof HttpErrorResponse && error . status === 401 ) {
51
- return this . handle401Error ( req , next ) ;
52
- }
53
-
54
- return throwError ( error ) ;
41
+ return from ( this . addAuthHeader ( req ) ) . pipe (
42
+ switchMap ( ( dupReq : any ) => {
43
+ return next . handle ( dupReq ) . pipe (
44
+ catchError ( ( error ) => {
45
+ if ( error instanceof HttpErrorResponse && error . status === 401 ) {
46
+ return this . handle401Error ( req , next ) ;
47
+ }
48
+
49
+ return throwError ( ( ) => error ) ;
50
+ } )
51
+ ) ;
55
52
} )
56
53
) ;
57
54
}
58
55
return next . handle ( req ) ;
59
56
}
60
57
61
- private handle401Error ( req : HttpRequest < any > , next : HttpHandler ) : Observable < HttpEvent < any > > {
62
- if ( ! this . isRefreshing ) {
63
- this . isRefreshing = true ;
64
- this . refreshTokenSubject . next ( null ) ;
65
-
66
- return this . authService . refreshTokens ( ) . pipe (
67
- switchMap ( ( token : any ) => {
68
- this . isRefreshing = false ;
69
-
70
- const dupReq = this . addAuthHeader ( req ) ;
71
- const tokens = this . authService . retrieveTokens ( ) ;
72
-
73
- if ( tokens ) {
74
- this . refreshTokenSubject . next ( tokens . access_token ) ;
75
- } // else {
76
- // this.logger.logError('No tokens found after refresh');
77
- // this.authService.logout();
78
- // return throwError('');
79
- //}
80
-
81
- return next . handle ( dupReq ) ;
82
- } ) ,
83
- catchError ( ( err ) => {
84
- this . isRefreshing = false ;
85
-
86
- this . authService . logout ( ) ;
87
- return throwError ( err ) ;
88
- } )
89
- ) ;
90
- }
91
-
92
- return this . refreshTokenSubject . pipe (
93
- filter ( token => token !== null ) ,
94
- take ( 1 ) ,
95
- switchMap ( ( token ) => next . handle ( this . addAuthHeader ( req ) ) )
58
+ private handle401Error (
59
+ req : HttpRequest < any > ,
60
+ next : HttpHandler
61
+ ) : Observable < HttpEvent < any > > {
62
+ return this . authService . refreshTokens ( ) . pipe (
63
+ switchMap ( ( response ) => this . addAuthHeader ( req ) ) ,
64
+ switchMap ( ( dupReq : any ) => {
65
+ return next . handle ( dupReq ) ;
66
+ } ) ,
67
+ catchError ( ( err ) => {
68
+ this . authService . logout ( ) ;
69
+ return throwError ( ( ) => err ) ;
70
+ } )
96
71
) ;
97
72
}
98
73
99
- private addAuthHeader ( request : HttpRequest < any > ) : HttpRequest < any > {
100
- const tokens = this . authService . retrieveTokens ( ) ;
74
+ private async addAuthHeader (
75
+ request : HttpRequest < any >
76
+ ) : Promise < HttpRequest < any > > {
77
+ const tokens = await this . authService . retrieveTokens ( ) ;
101
78
if ( tokens ) {
102
79
const dupReq = request . clone ( {
103
80
headers : request . headers . set (
@@ -112,51 +89,6 @@ export class HttpsRequestInterceptor implements HttpInterceptor {
112
89
return request ;
113
90
}
114
91
115
- private handleResponseError (
116
- error : HttpErrorResponse ,
117
- request : HttpRequest < any > ,
118
- next : HttpHandler
119
- ) : Observable < HttpEvent < any > > {
120
- if ( error . status === 400 ) {
121
- // Show message
122
- }
123
-
124
- // Unauthorized
125
- else if ( error . status === 401 ) {
126
- this . logger . logDebug (
127
- `In handleResponseError got 401 response: ${ request . url } `
128
- ) ;
129
-
130
- return timer ( 10000 ) . pipe (
131
- switchMap ( ( ) => {
132
- const dupReq = this . addAuthHeader ( request ) ;
133
-
134
- return next . handle ( dupReq ) ; //.pipe(delay(1500));
135
- } )
136
- ) ;
137
- }
138
-
139
- // Access denied error
140
- else if ( error . status === 403 ) {
141
- // Show message
142
- // Logout
143
- //this.logout();
144
- }
145
-
146
- // Server error
147
- else if ( error . status === 500 ) {
148
- // Show message
149
- }
150
-
151
- // Maintenance error
152
- else if ( error . status === 503 ) {
153
- // Show message
154
- // Redirect to the maintenance page
155
- }
156
-
157
- return throwError ( error ) ;
158
- }
159
-
160
92
private shouldAddTokenToRequest ( requestUrl : string ) : boolean {
161
93
if ( ! requestUrl . startsWith ( this . config . baseApiUrl ( ) ) ) {
162
94
return false ;
@@ -168,4 +100,4 @@ export class HttpsRequestInterceptor implements HttpInterceptor {
168
100
169
101
return true ;
170
102
}
171
- }
103
+ }
0 commit comments