source: Farmatiko/ClientApp/src/app/shared/interceptors/jwt.interceptor.ts@ db484c9

Last change on this file since db484c9 was ad60966, checked in by Mile Jankuloski <mile.jankuloski@…>, 4 years ago

Auth fixes, bug fixes etc.

  • Property mode set to 100644
File size: 851 bytes
Line 
1import { Injectable } from '@angular/core';
2import {
3 HttpRequest,
4 HttpHandler,
5 HttpEvent,
6 HttpInterceptor,
7} from '@angular/common/http';
8import { Observable } from 'rxjs';
9import { environment } from '../../../environments/environment';
10import { AuthService } from '../services/auth.service';
11
12@Injectable()
13export class JwtInterceptor implements HttpInterceptor {
14 constructor(private authService: AuthService) {}
15
16 intercept(
17 request: HttpRequest<unknown>,
18 next: HttpHandler
19 ): Observable<HttpEvent<unknown>> {
20 const accessToken = localStorage.getItem('access_token');
21 const isApiUrl = request.url.startsWith(environment.baseApiUrl);
22 if (accessToken && isApiUrl) {
23 request = request.clone({
24 setHeaders: { Authorization: `Bearer ${accessToken}` },
25 });
26 }
27
28 return next.handle(request);
29 }
30}
Note: See TracBrowser for help on using the repository browser.