Skip to content

Instantly share code, notes, and snippets.

@changhuixu
Last active August 6, 2020 00:24
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 changhuixu/ae9fbd3556b14c7ba11d11333c0461f7 to your computer and use it in GitHub Desktop.
Save changhuixu/ae9fbd3556b14c7ba11d11333c0461f7 to your computer and use it in GitHub Desktop.
@Injectable()
export class UnauthorizedInterceptor implements HttpInterceptor {
constructor(private authService: AuthService, private router: Router) {}
intercept(
request: HttpRequest<unknown>,
next: HttpHandler
): Observable<HttpEvent<unknown>> {
return next.handle(request).pipe(
catchError((err) => {
if (err.status === 401) {
this.authService.clearLocalStorage();
this.router.navigate(['login']);
}
if (!environment.production) {
console.error(err);
}
const error = (err && err.error && err.error.message) || err.statusText;
return throwError(error);
})
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment