interceptors work
This commit is contained in:
@ -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 { }
|
||||
|
16
hv2-ui/src/app/auth-handler.interceptor.spec.ts
Normal file
16
hv2-ui/src/app/auth-handler.interceptor.spec.ts
Normal file
@ -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();
|
||||
});
|
||||
});
|
28
hv2-ui/src/app/auth-handler.interceptor.ts
Normal file
28
hv2-ui/src/app/auth-handler.interceptor.ts
Normal file
@ -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<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
|
||||
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);
|
||||
}
|
||||
}
|
16
hv2-ui/src/app/error-handler.interceptor.spec.ts
Normal file
16
hv2-ui/src/app/error-handler.interceptor.spec.ts
Normal file
@ -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();
|
||||
});
|
||||
});
|
31
hv2-ui/src/app/error-handler.interceptor.ts
Normal file
31
hv2-ui/src/app/error-handler.interceptor.ts
Normal file
@ -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<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
|
||||
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)
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user