Skip to content

Instantly share code, notes, and snippets.

@harbirchahal
Last active May 14, 2019 14:15
Show Gist options
  • Save harbirchahal/0d463b64908f7751c10346a32f148f94 to your computer and use it in GitHub Desktop.
Save harbirchahal/0d463b64908f7751c10346a32f148f94 to your computer and use it in GitHub Desktop.
Unauthorized Request HTTP Interceptor
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { tap } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class UnauthorizedHttpInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler) {
return next.handle(req).pipe(tap(
(event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
// do stuff with response if you want
}
},
(err: any) => {
if (err instanceof HttpErrorResponse) {
if (err.status === 401 || err.status === 403) {
// TODO: handle unauthorized error response
}
}
}
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment