-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
734b481
commit 6f83fde
Showing
7 changed files
with
84 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export enum Role { | ||
ANONYMOUS = 0, | ||
ADMIN, | ||
USER, | ||
GUEST, | ||
SERVICE, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import {Role} from './role'; | ||
|
||
export interface User { | ||
name: string, | ||
domain: string, | ||
role: Role, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,22 @@ | ||
import { HTTP_INTERCEPTORS } from '@angular/common/http'; | ||
import {HTTP_INTERCEPTORS} from '@angular/common/http'; | ||
import {AuthInterceptor} from './auth.interceptor'; | ||
import {ReqTokenInterceptor} from './req-token.interceptor'; | ||
import {UnauthorizedInterceptor} from './unauthorized.interceptor'; | ||
|
||
export const httpInterceptorProviders = [ | ||
{ | ||
provide: HTTP_INTERCEPTORS, | ||
useClass: AuthInterceptor, | ||
multi: true, | ||
}, | ||
{ | ||
provide: HTTP_INTERCEPTORS, | ||
useClass: ReqTokenInterceptor, | ||
multi: true, | ||
}, | ||
{ | ||
provide: HTTP_INTERCEPTORS, | ||
useClass: AuthInterceptor, | ||
multi: true, | ||
}, | ||
{ | ||
provide: HTTP_INTERCEPTORS, | ||
useClass: ReqTokenInterceptor, | ||
multi: true, | ||
}, | ||
{ | ||
provide: HTTP_INTERCEPTORS, | ||
useClass: UnauthorizedInterceptor, | ||
multi: true, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
projects/core/src/lib/interceptors/unauthorized.interceptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http'; | ||
import {SessionService} from '../provider'; | ||
import {Router} from '@angular/router'; | ||
import {catchError, Observable, of, throwError} from 'rxjs'; | ||
import {Injectable} from '@angular/core'; | ||
|
||
@Injectable() | ||
export class UnauthorizedInterceptor implements HttpInterceptor { | ||
constructor(private router: Router, private session: SessionService) { | ||
} | ||
|
||
intercept(req: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> { | ||
return next.handle(req).pipe( | ||
catchError((error): Observable<HttpEvent<unknown>> => { | ||
if (error instanceof HttpErrorResponse && error.status === 401) { | ||
this.session.clearData(); | ||
this.router.navigateByUrl('/', {replaceUrl: true}); | ||
return of() | ||
} else { | ||
return throwError(error); | ||
} | ||
}) | ||
); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters