Skip to content

Commit 4f66fcd

Browse files
committed
updated jwt interceptor to only add auth header for requests to api url
1 parent d7b5ae8 commit 4f66fcd

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/app/_helpers/jwt.interceptor.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@ import { Injectable } from '@angular/core';
22
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
33
import { Observable } from 'rxjs';
44

5+
import { environment } from '@environments/environment';
56
import { AuthenticationService } from '@app/_services';
67

78
@Injectable()
89
export class JwtInterceptor implements HttpInterceptor {
910
constructor(private authenticationService: AuthenticationService) { }
1011

1112
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
12-
// add authorization header with jwt token if available
13-
let currentUser = this.authenticationService.currentUserValue;
14-
if (currentUser && currentUser.token) {
13+
// add auth header with jwt if user is logged in and request is to api url
14+
const currentUser = this.authenticationService.currentUserValue;
15+
const isLoggedIn = currentUser && currentUser.token;
16+
const isApiUrl = request.url.startsWith(environment.apiUrl);
17+
if (isLoggedIn && isApiUrl) {
1518
request = request.clone({
1619
setHeaders: {
1720
Authorization: `Bearer ${currentUser.token}`

0 commit comments

Comments
 (0)