interceptors work

This commit is contained in:
2021-06-17 18:31:01 +02:00
parent 9547631c13
commit ce23d3921c
5 changed files with 98 additions and 2 deletions

View File

@ -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 { }

View 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();
});
});

View 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);
}
}

View 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();
});
});

View 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)
})
)
}
}