I've been working on creating an interceptor to handle a situation where a function needs to be called to refresh the session upon receiving a 401 error response. Here's what I have so far but I'm facing build issues and struggling to figure out how to make it work:
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
request = request.clone({
withCredentials: true
});
return next.handle(request).pipe(
catchError((error: HttpErrorResponse) => {
if (error.status === 401) {
return this.userAPI
.refreshSession(this.authService.getRefreshToken())
.then(res => {
this.authService.setAuthenticated(false);
return next.handle(request);
});
} else {
this.authService.setAuthenticated(false);
}
})
);
}
VS Code is showing this error message:
Type 'Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any> | Observable<HttpEvent<any>>' is not assignable to type 'Observable<HttpEvent<any>>'.