Last change
on this file since 28d7d35 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 | |
---|
1 | import { Injectable } from '@angular/core';
|
---|
2 | import {
|
---|
3 | HttpRequest,
|
---|
4 | HttpHandler,
|
---|
5 | HttpEvent,
|
---|
6 | HttpInterceptor,
|
---|
7 | } from '@angular/common/http';
|
---|
8 | import { Observable } from 'rxjs';
|
---|
9 | import { environment } from '../../../environments/environment';
|
---|
10 | import { AuthService } from '../services/auth.service';
|
---|
11 |
|
---|
12 | @Injectable()
|
---|
13 | export 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.