implement interceptor
This commit is contained in:
parent
7a07bf5b07
commit
33007acc77
|
@ -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]
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
</fieldset>
|
||||
<div class="row py-2">
|
||||
<div class="col-12 col-sm-6 col-md-6 mb-1">
|
||||
<a [routerLink]="['/dashboard/sales']" class="btn btn-primary btn-block"><i class="feather ft-home"></i>
|
||||
<a [routerLink]="['/monitoring']" class="btn btn-primary btn-block"><i class="feather ft-home"></i>
|
||||
Home</a>
|
||||
</div>
|
||||
<div class="col-12 col-sm-6 col-md-6 mb-1">
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
});
|
|
@ -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<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||
return next.handle(req).pipe(
|
||||
catchError((error: HttpErrorResponse) => {
|
||||
if (error.status === 403) {
|
||||
this.router.navigate(['/error/error403']);
|
||||
}
|
||||
return throwError(error);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue