|
| 1 | +import { HttpEvent, HttpRequest } from '@angular/common/http'; |
| 2 | +import { inject } from '@angular/core'; |
| 3 | +import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; |
| 4 | +import { Observable } from 'rxjs'; |
| 5 | +import { AuthGuard } from './auth.guard'; |
| 6 | +import { AuthHttpInterceptor } from './auth.interceptor'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Functional AuthGuard to ensure routes can only be accessed when authenticated. |
| 10 | + * |
| 11 | + * Note: Should only be used as of Angular 15 |
| 12 | + * |
| 13 | + * @param route Contains the information about a route associated with a component loaded in an outlet at a particular moment in time. |
| 14 | + * @param state Represents the state of the router at a moment in time. |
| 15 | + * @returns An Observable<Boolean>, indicating if the route can be accessed or not |
| 16 | + */ |
| 17 | +export const authGuardFn = ( |
| 18 | + route: ActivatedRouteSnapshot, |
| 19 | + state: RouterStateSnapshot |
| 20 | +) => inject(AuthGuard).canActivate(route, state); |
| 21 | + |
| 22 | +/** |
| 23 | + * Functional AuthHttpInterceptor to include the access token in matching requests. |
| 24 | + * |
| 25 | + * Note: Should only be used as of Angular 15 |
| 26 | + * |
| 27 | + * @param req An outgoing HTTP request with an optional typed body. |
| 28 | + * @param handle Represents the next interceptor in an interceptor chain, or the real backend if there are no |
| 29 | + * further interceptors. |
| 30 | + * @returns An Observable<HttpEvent<any>>, representing the intercepted HttpRequest |
| 31 | + */ |
| 32 | +export const authHttpInterceptorFn = ( |
| 33 | + req: HttpRequest<any>, |
| 34 | + handle: (req: HttpRequest<unknown>) => Observable<HttpEvent<unknown>> |
| 35 | +) => inject(AuthHttpInterceptor).intercept(req, { handle }); |
0 commit comments