Skip to content

Commit a9eb19a

Browse files
docs: update to use provideAuth0 and provideAppInitializer (#756)
1 parent 52aa2f0 commit a9eb19a

File tree

1 file changed

+70
-22
lines changed

1 file changed

+70
-22
lines changed

README.md

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,35 @@ Take note of the **Client ID** and **Domain** values under the "Basic Informatio
6464

6565
#### Static configuration
6666

67-
Install the SDK into your application by importing `AuthModule.forRoot()` and configuring with your Auth0 domain and client id, as well as the URL to which Auth0 should redirect back after succesful authentication:
67+
The recommended approach is to use the functional `provideAuth0()` in your application configuration:
68+
69+
```ts
70+
import { ApplicationConfig } from '@angular/core';
71+
import { provideAuth0 } from '@auth0/auth0-angular';
72+
73+
export const appConfig: ApplicationConfig = {
74+
providers: [
75+
provideAuth0({
76+
domain: 'YOUR_AUTH0_DOMAIN',
77+
clientId: 'YOUR_AUTH0_CLIENT_ID',
78+
authorizationParams: {
79+
redirect_uri: window.location.origin,
80+
},
81+
}),
82+
],
83+
};
84+
```
85+
86+
<details>
87+
<summary>Using NgModules</summary>
88+
89+
If you're using NgModules, you can configure the SDK using `AuthModule.forRoot()`:
6890

6991
```ts
7092
import { NgModule } from '@angular/core';
7193
import { AuthModule } from '@auth0/auth0-angular';
7294

7395
@NgModule({
74-
// ...
7596
imports: [
7697
AuthModule.forRoot({
7798
domain: 'YOUR_AUTH0_DOMAIN',
@@ -81,53 +102,80 @@ import { AuthModule } from '@auth0/auth0-angular';
81102
},
82103
}),
83104
],
84-
// ...
85105
})
86106
export class AppModule {}
87107
```
88108

109+
</details>
110+
89111
#### Dynamic configuration
90112

91-
Instead of using `AuthModule.forRoot` to specify auth configuration, you can provide a factory function using `APP_INITIALIZER` to load your config from an external source before the auth module is loaded, and provide your configuration using `AuthClientConfig.set`.
113+
Instead of providing static configuration, you can use `provideAppInitializer` to load your config from an external source before the SDK is instantiated, and configure it using `AuthClientConfig.set`.
92114

93-
The configuration will only be used initially when the SDK is instantiated. Any changes made to the configuration at a later moment in time will have no effect on the default options used when calling the SDK's methods. This is also the reason why the dynamic configuration should be set using an `APP_INITIALIZER`, because doing so ensures the configuration is available prior to instantiating the SDK.
115+
```ts
116+
import { ApplicationConfig, inject, provideAppInitializer } from '@angular/core';
117+
import { provideHttpClient, HttpBackend, HttpClient } from '@angular/common/http';
118+
import { provideAuth0, AuthClientConfig } from '@auth0/auth0-angular';
94119

95-
> :information_source: Any request made through an instance of `HttpClient` that got instantiated by Angular, will use all of the configured interceptors, including our `AuthHttpInterceptor`. Because the `AuthHttpInterceptor` requires the existence of configuration settings, the request for retrieving those dynamic configuration settings should ensure it's not using any of those interceptors. In Angular, this can be done by manually instantiating `HttpClient` using an injected `HttpBackend` instance.
120+
export function configInitializer(handler: HttpBackend, config: AuthClientConfig) {
121+
return () =>
122+
new HttpClient(handler)
123+
.get('/config')
124+
.toPromise()
125+
.then((loadedConfig: any) => config.set(loadedConfig));
126+
}
127+
128+
export const appConfig: ApplicationConfig = {
129+
providers: [
130+
provideHttpClient(),
131+
provideAuth0(),
132+
provideAppInitializer(() => {
133+
const handler = inject(HttpBackend);
134+
const config = inject(AuthClientConfig);
135+
return configInitializer(handler, config)();
136+
}),
137+
],
138+
};
139+
```
140+
141+
> **Important:** The configuration will only be used initially when the SDK is instantiated. Any changes made to the configuration at a later moment in time will have no effect on the default options used when calling the SDK's methods. This is why the dynamic configuration should be set using an app initializer, which ensures the configuration is available prior to instantiating the SDK.
142+
143+
> :information_source: Any request made through an instance of `HttpClient` that got instantiated by Angular will use all configured interceptors, including our `AuthHttpInterceptor`. Because the `AuthHttpInterceptor` requires the existence of configuration settings, the request for retrieving those dynamic configuration settings should ensure it's not using any interceptors. In Angular, this can be done by manually instantiating `HttpClient` using an injected `HttpBackend` instance.
144+
145+
<details>
146+
<summary>Using NgModules</summary>
96147

97-
```js
148+
Instead of using `AuthModule.forRoot` to specify auth configuration, you can provide a factory function using `APP_INITIALIZER` to load your config from an external source before the auth module is loaded, and provide your configuration using `AuthClientConfig.set`.
149+
150+
```ts
151+
import { APP_INITIALIZER } from '@angular/core';
152+
import { HttpClientModule, HttpClient, HttpBackend } from '@angular/common/http';
98153
import { AuthModule, AuthClientConfig } from '@auth0/auth0-angular';
99154

100-
// Provide an initializer function that returns a Promise
101-
function configInitializer(
102-
handler: HttpBackend,
103-
config: AuthClientConfig
104-
) {
155+
function configInitializer(handler: HttpBackend, config: AuthClientConfig) {
105156
return () =>
106157
new HttpClient(handler)
107158
.get('/config')
108159
.toPromise()
109-
// Set the config that was loaded asynchronously here
110160
.then((loadedConfig: any) => config.set(loadedConfig));
111161
}
112162

113-
export class AppModule {
114-
// ...
115-
imports: [
116-
HttpClientModule,
117-
AuthModule.forRoot(), // <- don't pass any config here
118-
],
163+
@NgModule({
164+
imports: [HttpClientModule, AuthModule.forRoot()],
119165
providers: [
120166
{
121167
provide: APP_INITIALIZER,
122-
useFactory: configInitializer, // <- pass your initializer function here
168+
useFactory: configInitializer,
123169
deps: [HttpBackend, AuthClientConfig],
124170
multi: true,
125171
},
126172
],
127-
// ...
128-
}
173+
})
174+
export class AppModule {}
129175
```
130176

177+
</details>
178+
131179
### Add login to your application
132180

133181
To log the user into the application, inject the `AuthService` and call its `loginWithRedirect` method.

0 commit comments

Comments
 (0)