Skip to content

Instantly share code, notes, and snippets.

@ThaneshR
Forked from chenkie/jwt.interceptor.ts
Created August 4, 2017 17:39
Show Gist options
  • Select an option

  • Save ThaneshR/ba894f6fcbf27a9caa6ff8c90a919f05 to your computer and use it in GitHub Desktop.

Select an option

Save ThaneshR/ba894f6fcbf27a9caa6ff8c90a919f05 to your computer and use it in GitHub Desktop.
import {
HttpInterceptor,
HttpRequest,
HttpResponse,
HttpHandler,
HttpEvent
} from '@angular/common/http';
import 'rxjs/add/operator/map';
@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
}
}, (err: any) => {
if (err instanceof HttpErrorResponse {
if (err.status === 401) {
// redirect to login
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment