Skip to content

Commit 06382a6

Browse files
Tiime-LcsGaLcsGa
authored andcommitted
feat(provide): improve the provide function in order to allow passing a factory function
1 parent 81dd79c commit 06382a6

File tree

2 files changed

+52
-24
lines changed

2 files changed

+52
-24
lines changed

EXAMPLES.md

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
# Examples using auth0-angular
22

3-
- [Add login to your application](#add-login-to-your-application)
4-
- [Add logout to your application](#add-logout-to-your-application)
5-
- [Checking if a user is authenticated](#checking-if-a-user-is-authenticated)
6-
- [Display the user profile](#display-the-user-profile)
7-
- [Protect a route](#protect-a-route)
8-
- [Call an API](#call-an-api)
9-
- [Handling errors](#handling-errors)
10-
- [Organizations](#organizations)
11-
- [Standalone Components and a more functional approach](#standalone-components-and-a-more-functional-approach)
3+
- [Examples using auth0-angular](#examples-using-auth0-angular)
4+
- [Add login to your application](#add-login-to-your-application)
5+
- [Add logout to your application](#add-logout-to-your-application)
6+
- [Checking if a user is authenticated](#checking-if-a-user-is-authenticated)
7+
- [Display the user profile](#display-the-user-profile)
8+
- [Protect a route](#protect-a-route)
9+
- [Call an API](#call-an-api)
10+
- [Specify the audience](#specify-the-audience)
11+
- [Register AuthHttpInterceptor](#register-authhttpinterceptor)
12+
- [Configure AuthHttpInterceptor to attach access tokens](#configure-authhttpinterceptor-to-attach-access-tokens)
13+
- [Handling errors](#handling-errors)
14+
- [Organizations](#organizations)
15+
- [Log in to an organization](#log-in-to-an-organization)
16+
- [Accept user invitations](#accept-user-invitations)
17+
- [Standalone components and a more functional approach](#standalone-components-and-a-more-functional-approach)
1218

1319
## Add login to your application
1420

@@ -157,7 +163,7 @@ import { AuthModule } from '@auth0/auth0-angular';
157163
clientId: 'YOUR_AUTH0_CLIENT_ID',
158164
authorizationParams: {
159165
audience: 'YOUR_AUTH0_API_IDENTIFIER',
160-
}
166+
},
161167
}),
162168
],
163169
// ...
@@ -278,7 +284,7 @@ AuthModule.forRoot({
278284
authorizationParams: {
279285
audience: 'http://my-api/',
280286
scope: 'write:orders',
281-
}
287+
},
282288
},
283289
},
284290
],
@@ -381,6 +387,7 @@ export class AppComponent {
381387
```
382388
383389
## Standalone components and a more functional approach
390+
384391
As of Angular 15, the Angular team is putting standalone components, as well as a more functional approach, in favor of the traditional use of NgModules and class-based approach.
385392
386393
There are a couple of difference with how you would traditionally implement our SDK:
@@ -398,18 +405,31 @@ const routes: Routes = [
398405
path: 'profile',
399406
component: ProfileComponent,
400407
canActivate: [authGuardFn],
401-
}
408+
},
402409
];
403410

404411
bootstrapApplication(AppComponent, {
405-
providers: [
406-
provideRouter(routes),
407-
provideAuth0(/* Auth Config Goes Here */),
408-
provideHttpClient(
409-
withInterceptors([authHttpInterceptorFn])
410-
)
411-
]
412+
providers: [provideRouter(routes), provideAuth0(/* Auth Config Goes Here */), provideHttpClient(withInterceptors([authHttpInterceptorFn]))],
412413
});
413414
```
414415
415416
Note that `provideAuth0` should **never** be provided to components, but only at the root level of your application.
417+
418+
`AuthConfig` can be omitted or it can be provided either as a basic config object, or a function that returns a config object:
419+
420+
```ts
421+
provideAuth0({
422+
clientId: 'clientId',
423+
domain: 'domain',
424+
}),
425+
// or
426+
provideAuth0(() => {
427+
const someProvider = inject(SomeProvider);
428+
// you can inject as many providers as you want
429+
return {
430+
clientId: 'clientId',
431+
domain: 'domain',
432+
// use someProvider (or other porviders) to build your config object
433+
};
434+
});
435+
```

projects/auth0-angular/src/lib/provide.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { AuthGuard } from './auth.guard';
55
import { AuthHttpInterceptor } from './auth.interceptor';
66
import { AuthService } from './auth.service';
77

8+
type AuthConfigFactory = () => AuthConfig;
9+
810
/**
911
* Initialize the authentication system. Configuration can either be specified here,
1012
* or by calling AuthClientConfig.set (perhaps from an APP_INITIALIZER factory function).
@@ -20,19 +22,25 @@ import { AuthService } from './auth.service';
2022
* ],
2123
* });
2224
*/
23-
export function provideAuth0(config?: AuthConfig): Provider[] {
25+
export function provideAuth0(
26+
config?: AuthConfig | AuthConfigFactory
27+
): Provider[] {
2428
return [
2529
AuthService,
2630
AuthHttpInterceptor,
2731
AuthGuard,
28-
{
29-
provide: AuthConfigService,
30-
useValue: config,
31-
},
32+
provideAuthConfigService(config),
3233
{
3334
provide: Auth0ClientService,
3435
useFactory: Auth0ClientFactory.createClient,
3536
deps: [AuthClientConfig],
3637
},
3738
];
3839
}
40+
41+
function provideAuthConfigService(config?: AuthConfig | AuthConfigFactory) {
42+
const provide = AuthConfigService;
43+
return typeof config === 'function'
44+
? { provide, useFactory: config }
45+
: { provide, useValue: config };
46+
}

0 commit comments

Comments
 (0)