diff --git a/EXAMPLES.md b/EXAMPLES.md index 5da255ea..19f06077 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -724,7 +724,7 @@ bootstrapApplication(AppComponent, { }); ``` -Note that `provideAuth0` should **never** be provided to components, but only at the root level of your application. +**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. ## Connect Accounts for using Token Vault diff --git a/projects/auth0-angular/src/lib/provide.ts b/projects/auth0-angular/src/lib/provide.ts index 00647915..92ea6abc 100644 --- a/projects/auth0-angular/src/lib/provide.ts +++ b/projects/auth0-angular/src/lib/provide.ts @@ -1,4 +1,4 @@ -import { Provider } from '@angular/core'; +import { EnvironmentProviders, makeEnvironmentProviders } from '@angular/core'; import { Auth0ClientService, Auth0ClientFactory } from './auth.client'; import { AuthConfig, AuthConfigService, AuthClientConfig } from './auth.config'; import { AuthGuard } from './auth.guard'; @@ -9,7 +9,9 @@ import { AuthService } from './auth.service'; * Initialize the authentication system. Configuration can either be specified here, * or by calling AuthClientConfig.set (perhaps from an APP_INITIALIZER factory function). * - * Note: Should only be used as of Angular 15, and should not be added to a component's providers. + * Note: Should only be used as of Angular 15. This function returns `EnvironmentProviders` + * which ensures it can only be used at the application/environment level and cannot be + * added to a component's providers array (this will result in a compile-time error). * * @param config The optional configuration for the SDK. * @@ -20,8 +22,8 @@ import { AuthService } from './auth.service'; * ], * }); */ -export function provideAuth0(config?: AuthConfig): Provider[] { - return [ +export function provideAuth0(config?: AuthConfig): EnvironmentProviders { + return makeEnvironmentProviders([ AuthService, AuthHttpInterceptor, AuthGuard, @@ -34,5 +36,5 @@ export function provideAuth0(config?: AuthConfig): Provider[] { useFactory: Auth0ClientFactory.createClient, deps: [AuthClientConfig], }, - ]; + ]); }