App initializer with 6.0.12
Hi, I was using 4.03 version of library, now I updated it to 6.0.12. I faced into one issue. Check the code.
{
// Load the server settings and get the capabilities of the authentication server
provide: APP_INITIALIZER,
useFactory: (configService: ConfigurationService, oidcConfigService: OidcConfigService) =>
() => {
return configService.loadConfigurationData().then((result) => {
console.log('[CONFIG] loadConfigurationData SUCCEED');
console.log('[CONFIG] oidcConfigService.load_using_stsServer');
return oidcConfigService.load_using_stsServer(configService.config.authServer).then((result2) => {
console.log('[CONFIG] oidcConfigService.load_using_stsServer SUCCEED');
}).catch((err) => {
console.error('load_using_stsServer');
});
}).catch((err) => {
console.error('loadConfigurationData');
});
},
deps: [ConfigurationService, OidcConfigService],
multi: true
},
The issue is next: Before, load_using_stsServer returned Promise, so returning promise in APP_INITIALIZE worked good, the app never start before loading configuration. Now it returns void, but we have onConfigurationLoaded event emmiter in OidcConfigService.
How I can change my code to work it like before? The issue is OidcConfiguration service loaded before the response from load_using sts returning.
Even if I set this.oidcConfigService.onConfigurationLoaded.subscribe( () => load_configuration...) in my authService, the problem is with authGuard. It launches the getIsAuthorized() method from the oidcSecurityService.
The main goal is never launch the app if load_using_sts is not completed (failed, pending). Can you help with this issue?
You should use the from rxjs function to convert your configService.loadConfigurationData() promise to an observable. At that point you can switch to the oidcConfigService.onConfigurationLoaded.
So something like this:
return () => from(configService.loadConfigurationData())
.pipe(
tap((result) => {
oidcConfigService.load_using_stsServer(configService.config.authServer);
}),
switchMapTo(oidcConfigService.onConfigurationLoaded())
)
.toPromise();
Hi, thank you for reply. First of all you have mistake in your code (oidcConfigService.onConfigurationLoaded() -> oidcConfigService.onConfigurationLoaded) This solution is not working (App is stucked and never loads). I guess it's because onConfigurationLoaded fires only one time and never again, so promise leave uncompleted. Returning it's just onConfigurationLoaded.toPromise() also not working. I created my own promise and subscribed to onConfigurationLoaded.
return configService.loadConfigurationData().then((result) => {
console.log('[CONFIG] loadConfigurationData SUCCEED');
console.log('[CONFIG] oidcConfigService.load_using_stsServer');
oidcConfigService.load_using_stsServer(configService.config.authServer);
const promise = new Promise((resolve, reject) => {
oidcConfigService.onConfigurationLoaded.subscribe(a => {
a && resolve()
})
});
return promise;
}).catch((err) => {
console.error('loadConfigurationData');
});
Maybe there is better solution?
closing this, please re-open is required