Skip to content

Commit

Permalink
use dynamic import for keycloak-admin-client package
Browse files Browse the repository at this point in the history
  • Loading branch information
Med5KDB committed Jul 21, 2024
1 parent 353f6ce commit 21f3418
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/providers/kc-admin-client.provider.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import { ConnectionConfig } from '@keycloak/keycloak-admin-client/lib/client';
import { Injectable } from '@nestjs/common';
import { KeycloakUserManagerModuleConfigOptions } from 'src/kc-user-manager.module-config-options';
import KeycloakAdminClient from '@keycloak/keycloak-admin-client';

@Injectable()
export class KCAdminClientProvider extends KeycloakAdminClient {
export class KCAdminClientProvider {
private KeycloakAdminClient: any;

constructor(private configOptions: KeycloakUserManagerModuleConfigOptions) {
const keycloakConfig: ConnectionConfig = {
baseUrl: configOptions.baseUrl,
realmName: configOptions.realm,
this.initializeClient();
}

async initializeClient() {
this.KeycloakAdminClient = (
await import('@keycloak/keycloak-admin-client')
).default; // Dynamic import
const keycloakConfig = {
baseUrl: this.configOptions.baseUrl,
realmName: this.configOptions.realm,
};
super(keycloakConfig);
this.authenticate();
this.authenticate(keycloakConfig);
}

async authenticate() {
await this.auth({
async authenticate(keycloakConfig: any) {
const client = new this.KeycloakAdminClient(keycloakConfig);
await client.auth({
grantType: 'client_credentials',
clientId: this.configOptions.clientId,
clientSecret: this.configOptions.clientSecret,
Expand Down

0 comments on commit 21f3418

Please sign in to comment.