File

src/lib/service/auth/auth-interceptor.ts

Index

Properties
Methods

Constructor

constructor(auth: AuthService)
Parameters :
Name Type Optional
auth AuthService no

Methods

intercept
intercept(request: HttpRequest, next: HttpHandler)
Parameters :
Name Type Optional
request HttpRequest<any> no
next HttpHandler no

Properties

Public auth
auth: AuthService
Type : AuthService
import {Injectable} from '@angular/core';
import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http';
import {Observable} from 'rxjs';
import {tap} from 'rxjs/operators';
import {AuthService} from './auth.service';

@Injectable()
export class AuthInterceptor implements HttpInterceptor {

  constructor(public auth: AuthService) {
  }

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    return next.handle(request).pipe(tap((event: HttpEvent<any>) => {
      if (event instanceof HttpResponse) {
        // do stuff with response if you want
      }
    }, (error: any) => {
      if (error instanceof HttpErrorResponse || error.isHttpErrorResponse) {
        if (error.status === 401) {
          this.auth.logout();
        }
      }
    }));
  }
}

results matching ""

    No results matching ""