aubium77
aubium77

Reputation: 118

Angular 6 and keycloak-angular module: How to load the keycloak url from a server

I'm using the keycloak-angular module (v 4.0.0) in an Angular 6 project. Now I'm trying to load the Url to the Keycloak to use, from my server (since it can be different depending on the apiKey). So, in the app initializer function I've tried doing something like this:

export function initializer(keycloak: KeycloakService, http: HttpClient): () => Promise<any> {
  return () => new Promise<boolean>((resolve) => {
    const apiKey = 'myapikey;
    // TODO: make KeycloakBearerInterceptor not intercept the HTTP-Call
    // load configuration from server
    const promise = http.get<ApiConfiguration>(environment.apiHost + '/1.0/configuration?apiKey=' + apiKey).toPromise()
      .then((data: ApiConfiguration) => {

        const keycloakConfig = {
          url: data.idpConfig.authority,   <-- This comes from the server
          realm: 'mediakey',
          clientId: 'mediakey'
        };
        keycloak.init({
          config: {
            url: data.idpConfig.authority,
            realm: 'mediakey',
            clientId: 'mediakey'
          },
          loadUserProfileAtStartUp: true,
          initOptions: {
            onLoad: 'check-sso',
            // onLoad: 'login-required',
            checkLoginIframe: false,
            flow: 'implicit'
          },
          enableBearerInterceptor: false,
          bearerExcludedUrls: [
            '/assets',
            '/1.0/configuration'
          ],
        }).catch((reason) => console.log(reason));
        return true;
      });

    return promise;
  });
}

But the KeycloakBearerInterceptor intercepts the http-call because the default of enableBearerInterceptor is true. Of course, the keycloak-module is not initialized yet and therefore the interceptor fails with

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'authenticated' of undefined
TypeError: Cannot read property 'authenticated' of undefined
    at KeycloakBearerInterceptor.push../shared/core/rest/keycloak-bearer-interceptor.ts.KeycloakBearerInterceptor.intercept (keycloak-bearer-interceptor.ts:42)

I have also tried to dummy-initialize the keycloak-module but it always performs the onload-function and that can also not be disabled.

Has anyone any ideas how to achieve this? Thanks

Upvotes: 0

Views: 1981

Answers (1)

aubium77
aubium77

Reputation: 118

It turned out, that we have a KeycloakBearerInterceptor of our own, which is the one that kicks in and fails. So this has nothing to do with the keycloak-angular library.

Upvotes: 1

Related Questions