auth-expired.interceptor.ts 919B

1234567891011121314151617181920212223242526
  1. import { Injectable } from '@angular/core';
  2. import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpErrorResponse } from '@angular/common/http';
  3. import { Observable } from 'rxjs';
  4. import { tap } from 'rxjs/operators';
  5. import { LoginService } from 'app/core/login/login.service';
  6. @Injectable()
  7. export class AuthExpiredInterceptor implements HttpInterceptor {
  8. constructor(private loginService: LoginService) {}
  9. intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
  10. return next.handle(request).pipe(
  11. tap(
  12. (event: HttpEvent<any>) => {},
  13. (err: any) => {
  14. if (err instanceof HttpErrorResponse) {
  15. if (err.status === 401) {
  16. this.loginService.logout();
  17. }
  18. }
  19. }
  20. )
  21. );
  22. }
  23. }