Skip to content

Commit 8156aff

Browse files
committed
Added authentication error interceptor
1 parent 07c732d commit 8156aff

4 files changed

Lines changed: 49 additions & 2 deletions

File tree

src/app/authentication/authentication.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ import { CommonModule } from '@angular/common';
22
import { HTTP_INTERCEPTORS } from '@angular/common/http';
33
import { NgModule } from '@angular/core';
44
import { BasicAuthenticationInterceptor } from './interceptor/basic-authentication.interceptor';
5+
import { UnauthorizedInterceptor } from './interceptor/unauthorized.interceptor';
56

67
@NgModule({
78
declarations: [],
89
imports: [
910
CommonModule
1011
],
1112
providers: [
12-
{ provide: HTTP_INTERCEPTORS, useClass: BasicAuthenticationInterceptor, multi: true }
13+
{ provide: HTTP_INTERCEPTORS, useClass: BasicAuthenticationInterceptor, multi: true },
14+
{ provide: HTTP_INTERCEPTORS, useClass: UnauthorizedInterceptor, multi: true }
1315
]
1416
})
1517
export class AuthenticationModule { }

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ export class BasicAuthenticationInterceptor implements HttpInterceptor {
1111

1212
private tokenHeaderIdentifier = 'Basic'
1313

14-
constructor(private authenticationService: AuthenticationService) { }
14+
constructor(
15+
private authenticationService: AuthenticationService
16+
) { }
1517

1618
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
1719
let authReq;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { TestBed } from '@angular/core/testing';
2+
3+
import { UnauthorizedInterceptor } from './unauthorized.interceptor';
4+
5+
describe('UnauthorizedInterceptor', () => {
6+
beforeEach(() => TestBed.configureTestingModule({
7+
providers: [
8+
UnauthorizedInterceptor
9+
]
10+
}));
11+
12+
it('should be created', () => {
13+
const interceptor: UnauthorizedInterceptor = TestBed.inject(UnauthorizedInterceptor);
14+
expect(interceptor).toBeTruthy();
15+
});
16+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
2+
import { Injectable } from '@angular/core';
3+
import { catchError, Observable, throwError } from 'rxjs';
4+
import { AuthenticationService } from '../service/authentication.service';
5+
6+
@Injectable()
7+
export class UnauthorizedInterceptor implements HttpInterceptor {
8+
9+
constructor(
10+
private authenticationService: AuthenticationService
11+
) { }
12+
13+
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+
}
21+
22+
const error = err.error.message || err.statusText;
23+
return throwError(() => new Error(error));
24+
}))
25+
}
26+
27+
}

0 commit comments

Comments
 (0)