Skip to content

Commit

Permalink
Add AuthInterceptor for 401 Forbideen
Browse files Browse the repository at this point in the history
  • Loading branch information
GaetanF committed Oct 30, 2024
1 parent bdafe97 commit 7118b98
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/app/@cyborg/auth/auth.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Injectable } from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor,
HttpErrorResponse
} from '@angular/common/http';
import { Observable } from 'rxjs';
import {tap} from 'rxjs/operators';
import {Router} from '@angular/router';

@Injectable()
export class AuthInterceptor implements HttpInterceptor {

constructor(private router: Router) {}

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe( tap({
next: () => {},
error: (err: any) => {
if (err instanceof HttpErrorResponse) {
if (err.status !== 401) {
return;
}
this.router.navigate(['auth', 'login']);
}
}}));
}
}
6 changes: 6 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {ServerInterceptor} from './@cyborg/auth/server.interceptor';
import {NB_AUTH_TOKEN_INTERCEPTOR_FILTER} from '@nebular/auth';
import {CyborgModule} from './@cyborg/cyborg.module';
import {HttpXSRFInterceptor} from './@cyborg/auth/xsrf.interceptor';
import {AuthInterceptor} from "./@cyborg/auth/auth.interceptor";

@NgModule({
declarations: [
Expand Down Expand Up @@ -74,6 +75,11 @@ import {HttpXSRFInterceptor} from './@cyborg/auth/xsrf.interceptor';
useClass: ServerInterceptor,
multi: true
},
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true
},
{
provide: NB_AUTH_TOKEN_INTERCEPTOR_FILTER,
useValue: (req) => req.url.indexOf('/api/v1/') !== -1,
Expand Down

0 comments on commit 7118b98

Please sign in to comment.