All files / src/app/http-interceptors auth.interceptor.ts

100% Statements 9/9
100% Branches 2/2
100% Functions 3/3
100% Lines 8/8

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            1498x 1498x   1498x 119x     1379x       1379x       1379x     122x        
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);
    })
  );
}