-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
51 changed files
with
1,345 additions
and
1,066 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,4 +52,4 @@ | |
] | ||
} | ||
] | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,62 +1,46 @@ | ||
import { | ||
HttpErrorResponse, | ||
HttpEvent, | ||
HttpHandlerFn, | ||
HttpHandler, | ||
HttpInterceptor, | ||
HttpRequest, | ||
HttpErrorResponse, | ||
} from '@angular/common/http'; | ||
import { inject } from '@angular/core'; | ||
import { Injectable } from '@angular/core'; | ||
import { throwError, Observable } from 'rxjs'; | ||
import { catchError } from 'rxjs/operators'; | ||
import { AuthService } from 'app/core/auth/auth.service'; | ||
import { AuthUtils } from 'app/core/auth/auth.utils'; | ||
import { catchError, Observable, throwError } from 'rxjs'; | ||
|
||
/** | ||
* Intercept | ||
* | ||
* @param req | ||
* @param next | ||
*/ | ||
export const authInterceptor = ( | ||
req: HttpRequest<unknown>, | ||
next: HttpHandlerFn | ||
): Observable<HttpEvent<unknown>> => { | ||
const authService = inject(AuthService); | ||
|
||
// Clone the request object | ||
let newReq = req.clone(); | ||
@Injectable() | ||
export class AuthInterceptor implements HttpInterceptor { | ||
constructor(private authService: AuthService) {} | ||
|
||
// Request | ||
// | ||
// If the access token didn't expire, add the Authorization header. | ||
// We won't add the Authorization header if the access token expired. | ||
// This will force the server to return a "401 Unauthorized" response | ||
// for the protected API routes which our response interceptor will | ||
// catch and delete the access token from the local storage while logging | ||
// the user out from the app. | ||
if ( | ||
authService.accessToken && | ||
!AuthUtils.isTokenExpired(authService.accessToken) | ||
) { | ||
newReq = req.clone({ | ||
headers: req.headers.set( | ||
'Authorization', | ||
'Bearer ' + authService.accessToken | ||
), | ||
}); | ||
} | ||
|
||
// Response | ||
return next(newReq).pipe( | ||
catchError(error => { | ||
// Catch "401 Unauthorized" responses | ||
if (error instanceof HttpErrorResponse && error.status === 401) { | ||
// Sign out | ||
authService.signOut(); | ||
intercept( | ||
req: HttpRequest<any>, | ||
next: HttpHandler | ||
): Observable<HttpEvent<any>> { | ||
let newReq = req; | ||
|
||
// Reload the app | ||
location.reload(); | ||
} | ||
if ( | ||
this.authService.accessToken && | ||
!AuthUtils.isTokenExpired(this.authService.accessToken) | ||
) { | ||
newReq = req.clone({ | ||
headers: req.headers.set( | ||
'Authorization', | ||
'Bearer ' + this.authService.accessToken | ||
), | ||
}); | ||
} | ||
|
||
return throwError(() => new Error(error)); | ||
}) | ||
); | ||
}; | ||
return next.handle(newReq).pipe( | ||
catchError((error: HttpErrorResponse) => { | ||
if (error.status === 401) { | ||
this.authService.signOut(); | ||
location.reload(); | ||
} | ||
return throwError(() => new Error(error.message)); | ||
}) | ||
); | ||
} | ||
} |
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,18 @@ | ||
import { provideHttpClient, withInterceptors } from '@angular/common/http'; | ||
import { ENVIRONMENT_INITIALIZER, EnvironmentProviders, inject, Provider } from '@angular/core'; | ||
import { authInterceptor } from 'app/core/auth/auth.interceptor'; | ||
import { HTTP_INTERCEPTORS } from '@angular/common/http'; | ||
import { Provider } from '@angular/core'; | ||
import { AuthInterceptor } from 'app/core/auth/auth.interceptor'; | ||
import { AuthService } from 'app/core/auth/auth.service'; | ||
|
||
export const provideAuth = (): Array<Provider | EnvironmentProviders> => | ||
{ | ||
return [ | ||
provideHttpClient(withInterceptors([authInterceptor])), | ||
{ | ||
provide : ENVIRONMENT_INITIALIZER, | ||
useValue: () => inject(AuthService), | ||
multi : true, | ||
}, | ||
]; | ||
export const provideAuth = (): Array<Provider> => { | ||
return [ | ||
{ | ||
provide: HTTP_INTERCEPTORS, | ||
useClass: AuthInterceptor, | ||
multi: true, | ||
}, | ||
{ | ||
provide: AuthService, | ||
useClass: AuthService, | ||
}, | ||
]; | ||
}; |
Oops, something went wrong.