I am trying to implement an interceptor for failed requests in case of a token expired. I have tried multiple approaches but still no success.
I have referred below URL & read multiple blogs. everyone is suggesting switch map approach but not working with me. Angular 4 Interceptor retry requests after token refresh
I have tried collecting all request into cachedRequests: Array> = []; but not idea how to use it for re trigger failed requests .
this.next.handle(request) is not recalling after successful refreshtoken. I can see the updated request object though.
can someone please point to where to refer in angular 7 with
"rxjs": "~6.3.3", "rxjs-compat": "^6.5.2", to implement interceptor.
Below is the code working for me but getting response of token only
``````````
handleError(request: HttpRequest<any>, next: HttpHandler, err) {
if (err instanceof HttpErrorResponse) {
if (err.status === 401) {
this.service.getNewToken().pipe(filter(token => token != null),
take(1),
switchMap((token: any) => {
console.log('token', token);
if (token && token.access_token && token.refresh_token) {
sessionStorage.setItem('accessToken', token.access_token);
sessionStorage.setItem('refreshToken', token.refresh_token);
}
request = this.addHeader(request);
request.headers.getAll('Authorization');
return next.handle(request);
})).subscribe(results => this.subject.next(results));
}
}
}
`````````
Update 2: after trying multiple approaches I have found
- return next.handle(request); <-- this is simply not getting called
- return next.handle(request).subscribe(); <-- this is getting called but not updating response to requested component.
pointers appreciated