Skip to content

Commit

Permalink
bugfix: fix issue #600, keycloak initialization (#602)
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciovigolo authored Dec 24, 2024
1 parent 81dede8 commit 982677a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
7 changes: 3 additions & 4 deletions projects/keycloak-angular/src/lib/provide-keycloak.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -127,7 +127,7 @@ describe('provideKeycloak', () => {
};

const initOptions: KeycloakInitOptions = {
onLoad: 'login-required'
onLoad: 'check-sso'
};

const withCustomFeature: KeycloakFeature = {
Expand All @@ -144,7 +144,6 @@ describe('provideKeycloak', () => {
TestBed.configureTestingModule({
providers: [provideKeycloakAngular]
});

const keycloak = TestBed.inject(Keycloak);

expect(keycloak).toBeDefined();
Expand Down
20 changes: 13 additions & 7 deletions projects/keycloak-angular/src/lib/provide-keycloak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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));
});
};

Expand Down

0 comments on commit 982677a

Please sign in to comment.