keycloak-angular icon indicating copy to clipboard operation
keycloak-angular copied to clipboard

Features are not configured when manually managing keycloak.init()

Open erikm-of opened this issue 9 months ago • 0 comments

The documentation states the following: 'If you need to control when keycloak.init is called, do not pass the initOptions to the provideKeycloak function. In this case, you are responsible for calling keycloak.init manually.'

However, if you don't configure initOptions in the provideKeycloak function, then features will not be enabled.

See the following piece of code:


const provideKeycloakInAppInitializer = (
  keycloak: Keycloak,
  options: ProvideKeycloakOptions,
): EnvironmentProviders | Provider[] => {
  debugger;
  const { initOptions, features = [] } = options;

  if (!initOptions) {
    return [];
  }

  return provideAppInitializer(async () => {
    const injector = inject(EnvironmentInjector);
    runInInjectionContext(injector, () => {
      console.log('features', features);
      features.forEach((feature) => feature.configure());
    });
    await keycloak.init(initOptions).catch((error) => console.error('Keycloak initialization failed', error));
  });
};

In this function, if the initOptions are empty, the intializer function is not called and will not enable the features as configured in the provideKeycloak function.

A typical use case that we see for several of our applications, is that we already have a provideAppInitializer function, which triggers some API calls for which authorization is required. Angular will handle all initializer functions in parallel, therefore our own initializer and the keycloak initializer are run in parallel. We fixed this by manually calling the keycloak.init() in our own initializer function, but now the features are not enabled.

erikm-of avatar Mar 20 '25 12:03 erikm-of