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

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

Jwt token auth interceptors, services and guards

  • 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 { AuthService } from '../services/auth.service';
10import { environment } from '../../../environments/environment';
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.