okta/okta-angular

Unable to provide a value for OKTA_CONFIG via factory provider that references a dependency from an APP_INITIALIZER

tjandro93 opened this issue · 8 comments

Describe the bug?

Our team is trying to upgrade from okta/okta-angular 3.1.0 to 6.0.0 and are running into an issue that is blocking us from moving forward.

Our app uses Angular's APP_INITIALIZER provider feature to load configuration from our API immediately on startup. Part of this configuration is used for getting the value to be provided for OKTA_CONFIG via a factory provider.

What we're seeing is that the OKTA_CONFIG factory provider is running before our APP_INITIALIZER provider is even invoked. Because of this we don't have our config values yet so the OktaAuth constructor throws an error.

Looking at the source on your GitHub for the library I believe the OKTA_CONFIG factory provider is being invoked because the constructor for the OktaAuthModule injects that token. It's my impression that NgModules have their constructors called very early in Angular's bootstrapping which may explain why the APP_INITIALIZER hasn't been invoked at this point yet.

Is there any way that OktaAuthModule's constructor can avoid having to inject services? I'm not sure I see a path around this issue otherwise. All the documentation I've read for the library mainly talks about just providing the config values as constants but we don't have that convenience in our application because we have multiple environments with differing configurations supplied by our API.

What is expected to happen?

I would expect that the library supports some way to initialize OktaAuth asynchronously or at least at a later time than during the Angular bootstrapping.

What is the actual behavior?

I am unable to initialize OktaAuth config values after fetching them from our API.

Reproduction Steps?

master in this repo shows the error. The working-in-v-3-1-0 branch shows the code working in the older library version.

SDK Versions

Our project currently using Angular 12.

For Okta dependencies we are upgrading from:

  • @okta/okta-angular 3.1.0
  • @okta/okta-signin-widget 5.0.0

To:

  • @okta/okta-angular 6.0.0
  • @okta/okta-auth-js 7.2.0
  • @okta/okta-signin-widget 7.0.0

Execution Environment

All browsers

Additional Information?

No response

Thanks for submitting this issue.
An ability to load and use Okta configuration asynchronously should be added to this library.
Internal ref: OKTA-584279

@jaredperreault-okta Appreciate the link. I found a Stack Overflow thread for a similar issue in an Auth0 library that suggested something along those lines as well.

Unfortunately our app uses a good amount of Angular features in the service that fetches our configuration so moving the config initialization to be before bootstrapping Angular isn't really a straight forward option for our codebase.

@denysoblohin-okta @jaredperreault-okta Wanted to follow up on this. Will this be addressed in an upcoming release?

We are actively looking into this, however it does seem like this will be a breaking change (and therefore require a major version release). We will have to refactor our logic a bit to no longer rely on the OktaAuth constructor

@tjandro93 Could you please checkout branch od-config-service-OKTA-584279 (#124) and try it in your project to see if new changes work for you?

@denysoblohin-okta Looks like this will work!

@tjandro93 Please use 6.2.0