Skip to content

Commit d6cd49a

Browse files
feat: enforce type safety for provideAuth0 using makeEnvironmentProviders
Update provideAuth0 to return EnvironmentProviders instead of Provider[], preventing incorrect usage in component providers at compile-time. This follows Angular best practices and provides immediate TypeScript feedback when the function is used incorrectly.
1 parent 37effa6 commit d6cd49a

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

EXAMPLES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ bootstrapApplication(AppComponent, {
724724
});
725725
```
726726

727-
Note that `provideAuth0` should **never** be provided to components, but only at the root level of your application.
727+
**Important:** `provideAuth0` returns `EnvironmentProviders`, which ensures it can only be used at the application/environment level. Attempting to add it to a component's `providers` array will result in a compile-time error.
728728

729729
## Connect Accounts for using Token Vault
730730

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Provider } from '@angular/core';
1+
import { EnvironmentProviders, makeEnvironmentProviders } from '@angular/core';
22
import { Auth0ClientService, Auth0ClientFactory } from './auth.client';
33
import { AuthConfig, AuthConfigService, AuthClientConfig } from './auth.config';
44
import { AuthGuard } from './auth.guard';
@@ -9,7 +9,9 @@ import { AuthService } from './auth.service';
99
* Initialize the authentication system. Configuration can either be specified here,
1010
* or by calling AuthClientConfig.set (perhaps from an APP_INITIALIZER factory function).
1111
*
12-
* Note: Should only be used as of Angular 15, and should not be added to a component's providers.
12+
* Note: Should only be used as of Angular 15. This function returns `EnvironmentProviders`
13+
* which ensures it can only be used at the application/environment level and cannot be
14+
* added to a component's providers array (this will result in a compile-time error).
1315
*
1416
* @param config The optional configuration for the SDK.
1517
*
@@ -20,8 +22,8 @@ import { AuthService } from './auth.service';
2022
* ],
2123
* });
2224
*/
23-
export function provideAuth0(config?: AuthConfig): Provider[] {
24-
return [
25+
export function provideAuth0(config?: AuthConfig): EnvironmentProviders {
26+
return makeEnvironmentProviders([
2527
AuthService,
2628
AuthHttpInterceptor,
2729
AuthGuard,
@@ -34,5 +36,5 @@ export function provideAuth0(config?: AuthConfig): Provider[] {
3436
useFactory: Auth0ClientFactory.createClient,
3537
deps: [AuthClientConfig],
3638
},
37-
];
39+
]);
3840
}

0 commit comments

Comments
 (0)