One issue I am currently facing is the inability to intercept a 401 status and redirect to the login page, which is a common practice in most applications. My interceptor code seems pretty standard:
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({
withCredentials: true
});
return next.handle(request)
.pipe(
retry(0),
takeUntil(this.httpCancelService.onCancelPendingRequests()), // cancels out pending requests on route change
catchError((error: HttpErrorResponse) => {
if (error.status === 401) {
this.authenticationService.logout(); // logout in case of 401
} else if (request.url.indexOf("assets") == -1) {
this.alertService.error('An unexpected error occurred, please try again later');
}
return throwError(errorMessage);
})
)
}
Although the browser correctly captures the request as a 401 in the Network tab, within this Angular interceptor, 'error.status' returns as '0' and 'error.message' shows as 'Unknown Error'.