Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 2059x 2059x 2059x 146x 1913x 1913x 1913x 148x | import { HttpErrorResponse, HttpEvent, HttpHandlerFn, HttpHeaders, HttpRequest } from '@angular/common/http';
import { inject } from '@angular/core';
import { catchError, Observable, throwError } from 'rxjs';
import { TwinpadApiService } from '../services/twinpad-api.service';
export function authInterceptor(req: HttpRequest<unknown>, next: HttpHandlerFn): Observable<HttpEvent<unknown>> {
const api = inject(TwinpadApiService);
const token = api.getToken();
if(!token) {
return next(req);
}
const headers = new HttpHeaders({
Authorization: 'bearer '+ token
});
const newReq = req.clone({
headers
});
return next(newReq).pipe(
catchError((error: HttpErrorResponse) => {
// console.error('Error while requesting:', error);
return throwError(() => error);
})
);
}
|