github.com/in4it/ecs-deploy@v0.0.42-0.20240508120354-ed77ff16df25/webapp/src/app/interceptors/http-interceptor.ts (about)

     1  
     2  import { tap } from 'rxjs/operators';
     3  import { Injectable } from '@angular/core';
     4  import { HttpInterceptor, HttpHandler, HttpRequest, HttpEvent, HttpResponse, HttpErrorResponse } from '@angular/common/http';
     5  
     6  import { Observable } from 'rxjs';
     7  
     8  
     9  import { AlertService } from '../services/index';
    10  
    11  import { environment } from '../../environments/environment'
    12  
    13  @Injectable()
    14  export class AppHttpInterceptor implements HttpInterceptor {
    15  
    16    constructor( private alertService: AlertService) { }
    17  
    18  	intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    19  		return next.handle(request).pipe(tap((event: HttpEvent<any>) => {
    20  		  if (event instanceof HttpResponse) {
    21  		    // process successful responses here
    22  		  }
    23  		}, (error: any) => {
    24  		  if (error instanceof HttpErrorResponse) {
    25  		    if (error.status === 401) {
    26            localStorage.removeItem('token');
    27            if (environment.samlEnabled) {
    28              window.location.href = '/ecs-deploy/saml/acs'
    29              return
    30            } else {
    31              // show login
    32            }
    33            this.alertService.error("Token expired, log in again");
    34  		    } else if (error.status === 504) {
    35            this.alertService.error("Couldn't connect to the backend - try again later");
    36  		    }
    37  		  }
    38  		}));
    39  	}
    40  }