I am using Angular 7.2 MatDialog from Angular Material and rxjs 6. after the token expires, on the next http request, it brings the dialog up, but it does not submit the failed http request again. Although these suggestions are slightly different than what I need, I have tried this and this without luck. All that I need is that after the dialog closes, that it submit the failed request again, which is not happening at the moment. Can someone assist in finding what I am missing?
private refreshingToken: boolean = false;
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(this.setAuthorizationHeader(request))
.pipe(
catchError((error, caught) => {
if (error instanceof HttpErrorResponse) {
if (error.status == 401) {
if (this.refreshingToken) {
return empty();
} else {
this.refreshingToken = true;
setTimeout(() => {
const dialog = this.authenticationService.OpenLoginDialog();
if (dialog) {
const afterClosed$: Subscription = dialog
.afterClosed()
.pipe(
switchMap(() => {
return this.store.select(state => state.AccountState)
.pipe(
map(data => data.accountLoggedIn)
)
}),
switchMap((accountLoggedIn: boolean) => {
if (accountLoggedIn) {
return next.handle(this.setAuthorizationHeader(request));
}
}),
finalize(() => {
this.refreshingToken = false;
if (afterClosed$)
afterClosed$.unsubscribe();
})
)
.subscribe(afterClosedsubscribe => {
console.log({afterClosedsubscribe: afterClosedsubscribe});
});
}
});
}
}
}
return caught;
})
);
}
private setAuthorizationHeader(request: HttpRequest<any>): HttpRequest<any> {
const token = this.tokenService.Get();
if (token != null) {
request = request.clone({
setHeaders: {
Authorization: `${token.tokenType} ${token.accessToken}`
}
});
}
return request;
}