rdt-client/client/src/app/auth.interceptor.ts
2021-01-12 20:46:07 -07:00

23 lines
849 B
TypeScript

import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private router: Router) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).pipe(
catchError((error: HttpErrorResponse) => {
if (error && error.status === 402) {
this.router.navigate(['/setup']);
} else if (error && (error.status === 401 || error.status === 403)) {
this.router.navigate(['/login']);
}
return throwError(error);
})
);
}
}