Skip to content

Instantly share code, notes, and snippets.

@SamuelMarks
Forked from NetanelBasal/interceptor-response.ts
Last active August 29, 2017 06:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SamuelMarks/fec9a736764c49b4790adbb99aca54a5 to your computer and use it in GitHub Desktop.
Save SamuelMarks/fec9a736764c49b4790adbb99aca54a5 to your computer and use it in GitHub Desktop.
@Injectable()
class JWTInterceptor implements HttpInterceptor {
constructor(private router: Router) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).map((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
// do stuff with response if you want
}
}).catch(err => {
if (err instanceof HttpErrorResponse) {
if (err.status === 401) {
// JWT expired, go to login
// Observable.throw(err);
}
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment