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

[BE] 닉네임 유효성 검사 로직 추가 #226

Merged
merged 2 commits into from
Nov 28, 2024
Merged
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
22 changes: 17 additions & 5 deletions BE/src/auth/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,29 @@ export class UserService {
throw new NotFoundException('존재하지 않는 유저입니다.');
}

if (newName.replaceAll(/ /g, '').includes('익명의투자자')) {
throw new BadRequestException('사용 불가능한 문자가 포함되어 있습니다.');
this.validateName(newName);
await this.checkNameDuplicate(newName);

return this.userRepository.update({ id: userId }, { nickname: newName });
}

private validateName(nickname: string) {
const regex = /^[가-힣a-zA-Z0-9]+$/;
if (!regex.test(nickname)) {
throw new BadRequestException('한글, 영문, 숫자만 사용 가능합니다.');
}

if (nickname.includes('익명의투자자')) {
throw new BadRequestException('사용 불가능한 단어가 포함되어 있습니다.');
}
}

private async checkNameDuplicate(nickname: string) {
const isDuplicated = await this.userRepository.existsBy({
nickname: newName,
nickname,
});
if (isDuplicated) {
throw new BadRequestException('이미 존재하는 닉네임입니다.');
}

return this.userRepository.update({ id: userId }, { nickname: newName });
}
}
Loading