diff --git a/hv2-ui/src/app/app.module.ts b/hv2-ui/src/app/app.module.ts index e491c15..ca0a458 100644 --- a/hv2-ui/src/app/app.module.ts +++ b/hv2-ui/src/app/app.module.ts @@ -14,7 +14,9 @@ import { MessagesComponent } from './messages/messages.component'; import { AppRoutingModule } from './app-routing.module'; import { TestOutputComponent } from './test-output/test-output.component'; import { MatCardModule } from '@angular/material/card'; -import { HttpClientModule } from '@angular/common/http'; +import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http'; +import { ErrorHandlerInterceptor } from './error-handler.interceptor'; +import { AuthHandlerInterceptor } from './auth-handler.interceptor'; @NgModule({ declarations: [ @@ -36,7 +38,10 @@ import { HttpClientModule } from '@angular/common/http'; MatCardModule, AppRoutingModule ], - providers: [], + providers: [ + { provide: HTTP_INTERCEPTORS, useClass: ErrorHandlerInterceptor, multi: true }, + { provide: HTTP_INTERCEPTORS, useClass: AuthHandlerInterceptor, multi: true } + ], bootstrap: [AppComponent] }) export class AppModule { } diff --git a/hv2-ui/src/app/auth-handler.interceptor.spec.ts b/hv2-ui/src/app/auth-handler.interceptor.spec.ts new file mode 100644 index 0000000..89e03db --- /dev/null +++ b/hv2-ui/src/app/auth-handler.interceptor.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { AuthHandlerInterceptor } from './auth-handler.interceptor'; + +describe('AuthHandlerInterceptor', () => { + beforeEach(() => TestBed.configureTestingModule({ + providers: [ + AuthHandlerInterceptor + ] + })); + + it('should be created', () => { + const interceptor: AuthHandlerInterceptor = TestBed.inject(AuthHandlerInterceptor); + expect(interceptor).toBeTruthy(); + }); +}); diff --git a/hv2-ui/src/app/auth-handler.interceptor.ts b/hv2-ui/src/app/auth-handler.interceptor.ts new file mode 100644 index 0000000..f8ab2f1 --- /dev/null +++ b/hv2-ui/src/app/auth-handler.interceptor.ts @@ -0,0 +1,28 @@ +import { Injectable } from '@angular/core'; +import { + HttpRequest, + HttpHandler, + HttpEvent, + HttpInterceptor +} from '@angular/common/http'; +import { Observable } from 'rxjs'; +import { MessageService } from './message.service'; + +@Injectable() +export class AuthHandlerInterceptor implements HttpInterceptor { + + token: String = "abc" + + constructor(private messageService: MessageService) {} + + intercept(request: HttpRequest, next: HttpHandler): Observable> { + if (request.url.includes('api.hv.nober.de')) { + const clone = request.clone({ + setHeaders: { Authorization: `Bearer ${this.token}`} + }) + return next.handle(clone) + } + + return next.handle(request); + } +} diff --git a/hv2-ui/src/app/error-handler.interceptor.spec.ts b/hv2-ui/src/app/error-handler.interceptor.spec.ts new file mode 100644 index 0000000..aa99c94 --- /dev/null +++ b/hv2-ui/src/app/error-handler.interceptor.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { ErrorHandlerInterceptor } from './error-handler.interceptor'; + +describe('ErrorHandlerInterceptor', () => { + beforeEach(() => TestBed.configureTestingModule({ + providers: [ + ErrorHandlerInterceptor + ] + })); + + it('should be created', () => { + const interceptor: ErrorHandlerInterceptor = TestBed.inject(ErrorHandlerInterceptor); + expect(interceptor).toBeTruthy(); + }); +}); diff --git a/hv2-ui/src/app/error-handler.interceptor.ts b/hv2-ui/src/app/error-handler.interceptor.ts new file mode 100644 index 0000000..d7c1476 --- /dev/null +++ b/hv2-ui/src/app/error-handler.interceptor.ts @@ -0,0 +1,31 @@ +import { Injectable } from '@angular/core'; +import { + HttpRequest, + HttpHandler, + HttpEvent, + HttpInterceptor, + HttpErrorResponse +} from '@angular/common/http'; +import { Observable, throwError } from 'rxjs'; +import { Router } from '@angular/router'; +import { MessageService } from './message.service'; +import { catchError } from 'rxjs/operators'; + +@Injectable() +export class ErrorHandlerInterceptor implements HttpInterceptor { + + constructor(private messageService: MessageService, private router: Router) {} + + intercept(request: HttpRequest, next: HttpHandler): Observable> { + return next.handle(request).pipe( + catchError((errorResponse: HttpErrorResponse) => { + if (errorResponse.status === 401) { + this.router.navigateByUrl('/login') + } else { + this.messageService.add(`Intercepted http error: ${errorResponse}`) + } + return throwError(errorResponse) + }) + ) + } +}