28 lines
656 B
TypeScript
28 lines
656 B
TypeScript
import { Injectable } from '@angular/core';
|
|
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from "@angular/common/http";
|
|
import {Observable} from "rxjs";
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class AuthInterceptorService implements HttpInterceptor {
|
|
|
|
intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
|
|
|
const tokenId = localStorage.getItem('tokenId');
|
|
|
|
if (!!tokenId) {
|
|
const cloned = req.clone({
|
|
headers: req.headers.set("Authorization",
|
|
"Bearer " + tokenId)
|
|
});
|
|
return next.handle(cloned);
|
|
}
|
|
|
|
return next.handle(req);
|
|
}
|
|
|
|
|
|
constructor() { }
|
|
}
|