Recently, I have been in the process of upgrading from okta/okta-angular version 3.x to 5.x and encountered an unexpected bug.
Upon startup of the application, we utilized APP_INITIALIZER to trigger
appInitializerFactory(configService: ConfigService)
, which involves making an HTTP call to fetch configuration data.
The function responsible for this call is as follows:
public async loadConfig(): Promise<any> {
return this.httpClient.get('assets/config.json').pipe(settings => settings)
.toPromise()
.then(settings => {
this.config = settings as IAppConfig;
})
.catch(exception => {
console.log("Exception encountered while retreiving configuration");
});
}
Prior to migrating to okta 5.x, the APP_INITIALIZER would wait for the promise to resolve. However, after the update, it seems like other providers are now being resolved before the promise in APP_INITILIZER is completed.
This issue manifests downstream at the oktaInitializerFactory
, where the following code is executed:
public oktaConfig() {
return Object.assign({
onAuthRequired: (oktaAuth: OktaAuth, injector: Injector) => {
const router = injector.get(Router);
router.navigate(['/login']);
}
}, this.config.oktaConfig);
}
The problem arises because this.config.oktaConfig
returns as undefined
due to the APP_INITIALIZER not finishing its await operation.
Below is the full app.module.ts file for reference:
(insert updated code snippet here...)
Have you encountered any reasons why my APP_INITIALIZER might not be completing before other code runs? Oddly enough, reverting back to okta 3.x resolves this issue.