Skip to content

Commit f96cf36

Browse files
committed
Interceptors apply only to our API
1 parent 8156aff commit f96cf36

2 files changed

Lines changed: 39 additions & 17 deletions

File tree

src/app/authentication/interceptor/basic-authentication.interceptor.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
3+
import { environment } from '@environments/environment';
34
import { Observable } from 'rxjs';
45
import { AuthenticationService } from '../service/authentication.service';
56

@@ -18,16 +19,26 @@ export class BasicAuthenticationInterceptor implements HttpInterceptor {
1819
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
1920
let authReq;
2021

21-
// Acquire the current user token
22-
const token = this.authenticationService.getUser().token;
22+
const isApiUrl = request.url.startsWith(environment.apiUrl);
2323

24-
if (token) {
25-
// Has token
26-
// It is added to the request
27-
authReq = request.clone({ headers: request.headers.set(this.tokenHeaderKey, `${this.tokenHeaderIdentifier} ${token}`) });
24+
if (isApiUrl) {
25+
// It is a request to our API
26+
27+
// Acquire the current user token
28+
const logged = this.authenticationService.getUser().logged;
29+
const token = this.authenticationService.getUser().token;
30+
31+
if ((logged) && (token)) {
32+
// Has token
33+
// It is added to the request
34+
authReq = request.clone({ headers: request.headers.set(this.tokenHeaderKey, `${this.tokenHeaderIdentifier} ${token}`) });
35+
} else {
36+
// No token
37+
// No changes to request
38+
authReq = request;
39+
}
2840
} else {
29-
// No token
30-
// No changes to request
41+
// External API
3142
authReq = request;
3243
}
3344

src/app/authentication/interceptor/unauthorized.interceptor.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
3+
import { environment } from '@environments/environment';
34
import { catchError, Observable, throwError } from 'rxjs';
45
import { AuthenticationService } from '../service/authentication.service';
56

@@ -11,16 +12,26 @@ export class UnauthorizedInterceptor implements HttpInterceptor {
1112
) { }
1213

1314
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
14-
return next.handle(request).pipe(catchError(err => {
15-
if (err.status === 401) {
16-
// Unauthenticated
17-
// Logs out
18-
this.authenticationService.logout();
19-
location.reload();
20-
}
15+
return next.handle(request).pipe(catchError(error => {
16+
17+
const isApiUrl = request.url.startsWith(environment.apiUrl);
18+
19+
if (isApiUrl) {
20+
// It is a request to our API
2121

22-
const error = err.error.message || err.statusText;
23-
return throwError(() => new Error(error));
22+
if (error.status === 401) {
23+
// Unauthenticated
24+
// Logs out
25+
this.authenticationService.logout();
26+
location.reload();
27+
}
28+
29+
const errorMessage = error.error.message || error.statusText;
30+
return throwError(() => new Error(errorMessage));
31+
} else {
32+
// External API
33+
return throwError(() => error);
34+
}
2435
}))
2536
}
2637

0 commit comments

Comments
 (0)