27 lines
883 B
TypeScript
27 lines
883 B
TypeScript
|
import { HttpEvent, HttpHandler, HttpRequest } from '@angular/common/http';
|
||
|
import { Injectable } from '@angular/core';
|
||
|
import { Observable } from 'rxjs';
|
||
|
import { BaseInterceptor } from './base.interceptor';
|
||
|
import { InterceptorType } from './interceptor-type';
|
||
|
import { AuthService } from '../../../core/services/auth/auth.service';
|
||
|
|
||
|
@Injectable()
|
||
|
export class AuthTokenInterceptor extends BaseInterceptor {
|
||
|
|
||
|
constructor(
|
||
|
private authService: AuthService) { super(); }
|
||
|
|
||
|
get type(): InterceptorType { return InterceptorType.AuthToken; }
|
||
|
|
||
|
interceptRequest(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||
|
const authToken: string = this.authService.current() ? this.authService.current().token : null;
|
||
|
if (!authToken) { return next.handle(req); }
|
||
|
req = req.clone({
|
||
|
setHeaders: {
|
||
|
AuthToken: authToken
|
||
|
}
|
||
|
});
|
||
|
return next.handle(req);
|
||
|
}
|
||
|
}
|