All files / app/http auth.interceptor.ts

43.75% Statements 7/16
38.46% Branches 5/13
50% Functions 1/2
33.33% Lines 4/12

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  2x           2x     1x 1x                                
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { ConfigService } from '../service/config.service';
import { Store } from '../store/store';
 
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
 
  constructor(
    private config: ConfigService,
    private store: Store,
  ) {}
 
  intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
    const userTag = this.store.local.selectedUserTag;
    if (!this.config.token && !userTag) return next.handle(request);
    let headers = request.headers;
    if (this.config.token) {
      headers = headers.set('Authorization', 'Bearer ' + this.config.token);
    }
    if (userTag) {
      headers = headers.set('User-Tag', userTag);
    }
    return next.handle(request.clone({ headers }));
  }
}