|
1 | | -// import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; |
2 | | -// import { Injectable } from '@angular/core'; |
3 | | -// import { Router } from '@angular/router'; |
4 | | -// import { Observable, throwError } from 'rxjs'; |
5 | | -// import { catchError } from 'rxjs/operators'; |
6 | | -// import { RouteNames } from '../models/enums'; |
7 | | -// import { SnackbarService } from '../services/snackbar/snackbar.service'; |
| 1 | +import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; |
| 2 | +import { Injectable } from '@angular/core'; |
| 3 | +import { Router } from '@angular/router'; |
| 4 | +import { EMPTY, Observable, of, throwError } from 'rxjs'; |
| 5 | +import { catchError } from 'rxjs/operators'; |
| 6 | +import { RouteNames } from '../models/enums'; |
| 7 | +import { SnackbarService } from '../services/snackbar/snackbar.service'; |
| 8 | +import { ClearanceService } from '../services/clearance.service'; |
8 | 9 |
|
9 | | -// @Injectable() |
10 | | -// export class ErrorInterceptor implements HttpInterceptor { |
11 | | -// constructor(private snackbarService: SnackbarService, private router: Router) {} |
| 10 | +@Injectable() |
| 11 | +export class ErrorInterceptor implements HttpInterceptor { |
| 12 | + constructor( |
| 13 | + private snackbarService: SnackbarService, |
| 14 | + private router: Router, |
| 15 | + private clearanceService: ClearanceService |
| 16 | + ) {} |
12 | 17 |
|
13 | | -// intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { |
14 | | -// return next.handle(req).pipe( |
15 | | -// catchError((err: HttpErrorResponse) => { |
16 | | -// if (err.status === 401) { |
17 | | -// this.snackbarService.openInfoSnackbar('Please login again to continue'); |
18 | | -// this.router.navigate([`/${RouteNames.LANDINGPAGE_LOGIN_BASEROUTE}`]); |
19 | | -// } |
20 | | -// return throwError(err.error); |
21 | | -// }) |
22 | | -// ); |
23 | | -// } |
24 | | -// } |
| 18 | + intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { |
| 19 | + return next.handle(req).pipe( |
| 20 | + catchError((err: HttpErrorResponse) => { |
| 21 | + if (err.status === 401) { |
| 22 | + // JWT has expired - log the user out and redirect to login |
| 23 | + this.snackbarService.openInfoSnackbar('Your session has expired. Please login again to continue.'); |
| 24 | + |
| 25 | + // Clear all cached data and session storage |
| 26 | + this.clearanceService.clearServices(); |
| 27 | + |
| 28 | + // Redirect to login page |
| 29 | + this.router.navigate([`/${RouteNames.LANDINGPAGE_LOGIN_BASEROUTE}`]); |
| 30 | + |
| 31 | + // do not propagate the error to the next interceptor |
| 32 | + return EMPTY; |
| 33 | + } |
| 34 | + return throwError(err); |
| 35 | + }) |
| 36 | + ); |
| 37 | + } |
| 38 | +} |
0 commit comments