Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

27 строки
1010 B

  1. import { Injectable } from '@angular/core';
  2. import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
  3. import { Observable } from 'rxjs';
  4. import { environment } from '@environments/environment';
  5. import { AccountService } from '@app/_services';
  6. @Injectable()
  7. export class JwtInterceptor implements HttpInterceptor {
  8. constructor(private accountService: AccountService) { }
  9. intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
  10. // add auth header with jwt if user is logged in and request is to the api url
  11. const user = this.accountService.userValue;
  12. const isLoggedIn = user && user.token;
  13. const isApiUrl = request.url.startsWith(environment.apiUrl);
  14. if (isLoggedIn && isApiUrl) {
  15. request = request.clone({
  16. setHeaders: {
  17. Authorization: `Bearer ${user.token}`
  18. }
  19. });
  20. }
  21. return next.handle(request);
  22. }
  23. }