diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 5f93baa..bac0c8e 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -5,7 +5,7 @@ import { HammerGestureConfig } from '@angular/platform-browser'; import { ReactiveFormsModule } from '@angular/forms'; -import { HttpClientModule } from '@angular/common/http'; +import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http'; import { NgbModule, NgbCarouselConfig, @@ -68,6 +68,7 @@ import { ToastrModule } from 'ngx-toastr'; import { UserService } from './_api/user/user.service'; import { PrivacyPolicyComponent } from './login/privacy-policy/privacy-policy.component'; import { TermsConditionComponent } from './login/terms-condition/terms-condition.component'; +import { HttpErrorInterceptorService } from './interceptors/http-error-interceptor.service'; @NgModule({ imports: [ @@ -93,7 +94,7 @@ import { TermsConditionComponent } from './login/terms-condition/terms-condition LoadingBarRouterModule, BlockUIModule.forRoot({ template: BlockTemplateComponent - }) + }), ], declarations: [ AppComponent, @@ -136,6 +137,7 @@ import { TermsConditionComponent } from './login/terms-condition/terms-condition }, NgbCarouselConfig, NgbModalConfig, + { provide: HTTP_INTERCEPTORS, useClass: HttpErrorInterceptorService, multi: true } ], bootstrap: [AppComponent], exports: [RouterModule] diff --git a/src/app/content/full-pages/error/error403/error403.component.html b/src/app/content/full-pages/error/error403/error403.component.html index c9796f2..8df8e5c 100644 --- a/src/app/content/full-pages/error/error403/error403.component.html +++ b/src/app/content/full-pages/error/error403/error403.component.html @@ -24,7 +24,7 @@
- + Home
diff --git a/src/app/interceptors/http-error-interceptor.service.spec.ts b/src/app/interceptors/http-error-interceptor.service.spec.ts new file mode 100644 index 0000000..535feac --- /dev/null +++ b/src/app/interceptors/http-error-interceptor.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { HttpErrorInterceptorService } from './http-error-interceptor.service'; + +describe('HttpErrorInterceptorService', () => { + let service: HttpErrorInterceptorService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(HttpErrorInterceptorService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/interceptors/http-error-interceptor.service.ts b/src/app/interceptors/http-error-interceptor.service.ts new file mode 100644 index 0000000..0e1df03 --- /dev/null +++ b/src/app/interceptors/http-error-interceptor.service.ts @@ -0,0 +1,22 @@ +import { HttpErrorResponse, HttpEvent, HttpHandler, HttpRequest } from '@angular/common/http'; +import { Injectable } from '@angular/core'; +import { Router } from '@angular/router'; +import { Observable, catchError, throwError } from 'rxjs'; + +@Injectable({ + providedIn: 'root' +}) +export class HttpErrorInterceptorService { + + constructor(private router: Router) { } + intercept(req: HttpRequest, next: HttpHandler): Observable> { + return next.handle(req).pipe( + catchError((error: HttpErrorResponse) => { + if (error.status === 403) { + this.router.navigate(['/error/error403']); + } + return throwError(error); + }) + ); + } +}