-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added counter if username already exists and accessing KC for it
- Loading branch information
1 parent
7aa6dc5
commit 6710f26
Showing
3 changed files
with
40 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { KeycloakUserService, UserDo } from '../keycloak-administration/index.js'; | ||
import { DomainError, EntityNotFoundError } from '../../shared/error/index.js'; | ||
|
||
@Injectable() | ||
export class UserRepository { | ||
public constructor(private kcUserService: KeycloakUserService) {} | ||
|
||
public async usernameExists(username: string): Promise<boolean> { | ||
const searchResult: Result<UserDo<true>, DomainError> | { ok: false; error: DomainError } = | ||
await this.kcUserService.findOne({ username: username }); | ||
if (searchResult.ok) { | ||
return true; | ||
} else { | ||
if (searchResult.error instanceof EntityNotFoundError) { | ||
return false; | ||
} | ||
} | ||
throw searchResult.error; | ||
} | ||
|
||
public async getNextAvailableUsername(calculatedUsername: string): Promise<string> { | ||
if (!(await this.usernameExists(calculatedUsername))) { | ||
return calculatedUsername; | ||
} | ||
let counter: number = 1; | ||
while (await this.usernameExists(calculatedUsername + counter)) { | ||
counter = counter + 1; | ||
} | ||
return calculatedUsername + counter; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters