Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: fix keycloak async initialization #602

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading