angular-auth-oidc-client icon indicating copy to clipboard operation
angular-auth-oidc-client copied to clipboard

App initializer with 6.0.12

Open evoronovich opened this issue 8 years ago • 3 comments

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?

evoronovich avatar Nov 21 '18 11:11 evoronovich

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();

profet23 avatar Nov 29 '18 19:11 profet23

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?

evoronovich avatar Feb 01 '19 10:02 evoronovich

closing this, please re-open is required

damienbod avatar Apr 26 '19 06:04 damienbod