From 982677a38ac28e5cf090444475a844ffc3214540 Mon Sep 17 00:00:00 2001 From: Mauricio Vigolo Date: Tue, 24 Dec 2024 01:00:59 -0300 Subject: [PATCH] bugfix: fix issue #600, keycloak initialization (#602) --- .../src/lib/provide-keycloak.spec.ts | 7 +++---- .../src/lib/provide-keycloak.ts | 20 ++++++++++++------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/projects/keycloak-angular/src/lib/provide-keycloak.spec.ts b/projects/keycloak-angular/src/lib/provide-keycloak.spec.ts index 146e5d9..703b557 100644 --- a/projects/keycloak-angular/src/lib/provide-keycloak.spec.ts +++ b/projects/keycloak-angular/src/lib/provide-keycloak.spec.ts @@ -10,12 +10,12 @@ import Keycloak, { KeycloakConfig, KeycloakInitOptions } from 'keycloak-js'; import { TestBed } from '@angular/core/testing'; import { provideKeycloak } from './provide-keycloak'; +import { KeycloakFeature } from './features/keycloak.feature'; +import { createInterceptorCondition } from './interceptors/keycloak.interceptor'; import { INCLUDE_BEARER_TOKEN_INTERCEPTOR_CONFIG, IncludeBearerTokenCondition } from './interceptors/include-bearer-token.interceptor'; -import { createInterceptorCondition } from './interceptors/keycloak.interceptor'; -import { KeycloakFeature } from './features/keycloak.feature'; describe('provideKeycloak', () => { it('should instantiate keycloak and initialize it if initOptions is provided', () => { @@ -127,7 +127,7 @@ describe('provideKeycloak', () => { }; const initOptions: KeycloakInitOptions = { - onLoad: 'login-required' + onLoad: 'check-sso' }; const withCustomFeature: KeycloakFeature = { @@ -144,7 +144,6 @@ describe('provideKeycloak', () => { TestBed.configureTestingModule({ providers: [provideKeycloakAngular] }); - const keycloak = TestBed.inject(Keycloak); expect(keycloak).toBeDefined(); diff --git a/projects/keycloak-angular/src/lib/provide-keycloak.ts b/projects/keycloak-angular/src/lib/provide-keycloak.ts index 98bd848..7c8ef87 100644 --- a/projects/keycloak-angular/src/lib/provide-keycloak.ts +++ b/projects/keycloak-angular/src/lib/provide-keycloak.ts @@ -7,7 +7,15 @@ */ import Keycloak, { KeycloakConfig, KeycloakInitOptions } from 'keycloak-js'; -import { EnvironmentProviders, makeEnvironmentProviders, provideAppInitializer, Provider } from '@angular/core'; +import { + EnvironmentInjector, + EnvironmentProviders, + inject, + makeEnvironmentProviders, + provideAppInitializer, + Provider, + runInInjectionContext +} from '@angular/core'; import { createKeycloakSignal, KEYCLOAK_EVENT_SIGNAL } from './signals/keycloak-events-signal'; import { KeycloakFeature } from './features/keycloak.feature'; @@ -55,12 +63,10 @@ const provideKeycloakInAppInitializer = ( return []; } - return provideAppInitializer(() => { - keycloak.init(initOptions).catch((error) => { - console.error('Keycloak initialization failed', error); - return Promise.reject(error); - }); - features.forEach((feature) => feature.configure()); + return provideAppInitializer(async () => { + const injector = inject(EnvironmentInjector); + runInInjectionContext(injector, () => features.forEach((feature) => feature.configure())); + await keycloak.init(initOptions).catch((error) => console.error('Keycloak initialization failed', error)); }); };