File

src/app/auth/support/token.interceptor.ts

Index

Methods

Constructor

constructor(inj: Injector)
Parameters :
Name Type Optional Description
inj Injector

Methods

intercept
intercept(request: HttpRequest, next: HttpHandler)
Parameters :
Name Type Optional Description
request HttpRequest<any>
next HttpHandler
Returns : Observable<HttpEvent<any>>
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable, Injector } from '@angular/core';
import { Observable } from 'rxjs';
import { AuthService } from '../auth.service';

@Injectable()
export class TokenInterceptor implements HttpInterceptor {

  constructor(private inj: Injector) {
  }

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const auth = this.inj.get(AuthService);

    if (auth.xAuthToken) {
      request = request.clone({
        setHeaders: {
          'x-auth-token': `${auth.xAuthToken}`
        }
      });
    }
    return next.handle(request);
  }
}

results matching ""

    No results matching ""