27 lines
708 B
TypeScript
27 lines
708 B
TypeScript
|
|
import { Injectable } from '@angular/core';
|
||
|
|
import {
|
||
|
|
HttpRequest,
|
||
|
|
HttpHandler,
|
||
|
|
HttpEvent,
|
||
|
|
HttpInterceptor
|
||
|
|
} from '@angular/common/http';
|
||
|
|
import { AuthService } from '../auth/auth.service';
|
||
|
|
import { Observable } from 'rxjs';
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class AuthInterceptor implements HttpInterceptor {
|
||
|
|
constructor(
|
||
|
|
private auth$: AuthService
|
||
|
|
) { }
|
||
|
|
|
||
|
|
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
|
||
|
|
if (!request.url.includes('/auth/apiToken')) {
|
||
|
|
return next.handle(request.clone({
|
||
|
|
headers: request.headers.set('Authorization', `Bearer ${this.auth$.apiToken}`),
|
||
|
|
}));
|
||
|
|
} else {
|
||
|
|
return next.handle(request);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|