I'm trying to get RefreshToken before each request and if it has expired I need to refresh it. The interceptor calls the service's function but I never get the API call. Please help
my interceptor code
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (!this.authService.isAccessTokenExpired()) {
return next.handle(this.addCookiesToRequest(request));
}
if (this.refreshTokenInProgress) {
return this.refreshTokenSubject.pipe(
filter(result => result !== null),
take(1),
switchMap(() => next.handle(this.addCookiesToRequest(request)))
);
} else {
this.refreshTokenInProgress = true;
this.refreshTokenSubject.next(null);
return this.authService.refreshExpiredToken().pipe(
switchMap((token: any) => {
this.refreshTokenInProgress = false;
this.refreshTokenSubject.next(token);
return next.handle(this.addCookiesToRequest(request));
}),
catchError((error: any) => {
this.refreshTokenInProgress = false;
return Observable.throw(error);
})
);
}
}
}
public addCookiesToRequest(request: HttpRequest<any>): HttpRequest<any> {
return request.clone({
withCredentials: true
});
}
authService refreshToken code
refreshExpiredToken() {
return this.http.get(`${api_url}/token/refresh`, {
withCredentials: true,
observe: 'response'
}).pipe(
tap((res) => {
if (res.status >= 300 && res.status < 400) {
window.location.href = res.url;
}
}),
catchError((error) => {
if (error.status === 401) {
this.router.navigate(['/login']);
}
return of(error);
})
);
}
I even tried to add somewhere .subscribe(), but still nothing