diff --git a/src/keycloak-user-manager/keycloak-user-manager.service.ts b/src/keycloak-user-manager/keycloak-user-manager.service.ts index f9d50a6..5e6c21d 100644 --- a/src/keycloak-user-manager/keycloak-user-manager.service.ts +++ b/src/keycloak-user-manager/keycloak-user-manager.service.ts @@ -19,18 +19,22 @@ export class KeycloakUserManagerService { async createUser(data: UserCreateInput): Promise { const { firstName, lastName, username, email, password } = data; - const createdUser = await this.kcAdminClientProvider.users.create({ - firstName, - lastName, - username, - email, - enabled: true, - credentials: [{ type: 'password', value: password, temporary: true }], - }); + const createdUser = await this.kcAdminClientProvider + .getkCAdminClient() + .users.create({ + firstName, + lastName, + username, + email, + enabled: true, + credentials: [{ type: 'password', value: password, temporary: true }], + }); const userId = createdUser.id; - const fullUser = await this.kcAdminClientProvider.users.findOne({ - id: userId, - }); + const fullUser = await this.kcAdminClientProvider + .getkCAdminClient() + .users.findOne({ + id: userId, + }); if (!fullUser) { throw new NotFoundException(`User with ID ${userId} not found`); } @@ -42,7 +46,7 @@ export class KeycloakUserManagerService { data: UserUpdateInput, ): Promise { const { firstName, lastName, username, email } = data; - await this.kcAdminClientProvider.users.update( + await this.kcAdminClientProvider.getkCAdminClient().users.update( { id: where.id, }, @@ -54,24 +58,30 @@ export class KeycloakUserManagerService { }, ); const userId = where.id; - const fullUser = await this.kcAdminClientProvider.users.findOne({ - id: userId, - }); + const fullUser = await this.kcAdminClientProvider + .getkCAdminClient() + .users.findOne({ + id: userId, + }); if (!fullUser) { throw new NotFoundException(`User with ID ${userId} not found`); } return this.mapKeycloakUserToUser(fullUser); } async deleteUser(where: UserWhereUniqueInput): Promise<{ id: string }> { - await this.kcAdminClientProvider.users.del({ id: where.id }); + await this.kcAdminClientProvider + .getkCAdminClient() + .users.del({ id: where.id }); return { id: where.id }; } async findUserById(where: UserWhereUniqueInput): Promise { const userId = where.id; - const fullUser = await this.kcAdminClientProvider.users.findOne({ - id: userId, - }); + const fullUser = await this.kcAdminClientProvider + .getkCAdminClient() + .users.findOne({ + id: userId, + }); if (!fullUser) { throw new NotFoundException(`User with ID ${userId} not found`); } @@ -79,14 +89,18 @@ export class KeycloakUserManagerService { } async findManyUsers(): Promise { - const users = await this.kcAdminClientProvider.users.find(); + const users = await this.kcAdminClientProvider + .getkCAdminClient() + .users.find(); return Promise.all( users.map(async (user) => this.mapKeycloakUserToUser(user)), ); } async countUsers(): Promise { - const users = await this.kcAdminClientProvider.users.find(); + const users = await this.kcAdminClientProvider + .getkCAdminClient() + .users.find(); return users.length; } @@ -94,7 +108,7 @@ export class KeycloakUserManagerService { resetUserPasswordInput: ResetUserPasswordInput, ): Promise { const { id, password } = resetUserPasswordInput; - await this.kcAdminClientProvider.users.resetPassword({ + await this.kcAdminClientProvider.getkCAdminClient().users.resetPassword({ id, credential: { type: 'password', @@ -103,9 +117,11 @@ export class KeycloakUserManagerService { }, }); const userId = id; - const fullUser = await this.kcAdminClientProvider.users.findOne({ - id: userId, - }); + const fullUser = await this.kcAdminClientProvider + .getkCAdminClient() + .users.findOne({ + id: userId, + }); if (!fullUser) { throw new NotFoundException(`User with ID ${userId} not found`); } diff --git a/src/providers/kc-admin-client.provider.ts b/src/providers/kc-admin-client.provider.ts index d85d0fc..c430979 100644 --- a/src/providers/kc-admin-client.provider.ts +++ b/src/providers/kc-admin-client.provider.ts @@ -28,4 +28,7 @@ export class KCAdminClientProvider { clientSecret: this.configOptions.clientSecret, }); } + getkCAdminClient() { + return this.KeycloakAdminClient; + } }