Skip to content

Commit 4a528e8

Browse files
Add API for standalone and functional approaches (#445)
1 parent cefd338 commit 4a528e8

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 });
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Provider } from '@angular/core';
2+
import { Auth0ClientService, Auth0ClientFactory } from './auth.client';
3+
import { AuthConfig, AuthConfigService, AuthClientConfig } from './auth.config';
4+
import { AuthGuard } from './auth.guard';
5+
import { AuthHttpInterceptor } from './auth.interceptor';
6+
import { AuthService } from './auth.service';
7+
8+
/**
9+
* Initialize the authentication system. Configuration can either be specified here,
10+
* or by calling AuthClientConfig.set (perhaps from an APP_INITIALIZER factory function).
11+
*
12+
* Note: Should only be used as of Angular 15, and should not be added to a component's providers.
13+
*
14+
* @param config The optional configuration for the SDK.
15+
*
16+
* @example
17+
* bootstrapApplication(AppComponent, {
18+
* providers: [
19+
* provideAuth0(),
20+
* ],
21+
* });
22+
*/
23+
export function provideAuth0(config?: AuthConfig): Provider[] {
24+
return [
25+
AuthService,
26+
AuthHttpInterceptor,
27+
AuthGuard,
28+
{
29+
provide: AuthConfigService,
30+
useValue: config,
31+
},
32+
{
33+
provide: Auth0ClientService,
34+
useFactory: Auth0ClientFactory.createClient,
35+
deps: [AuthClientConfig],
36+
},
37+
];
38+
}

projects/auth0-angular/src/public-api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export * from './lib/auth.config';
1010
export * from './lib/auth.client';
1111
export * from './lib/auth.state';
1212
export * from './lib/interfaces';
13+
export * from './lib/provide';
14+
export * from './lib/functional';
1315

1416
export {
1517
AuthorizationParams,

0 commit comments

Comments
 (0)