Skip to content

Commit d68a20e

Browse files
authored
[Feat] 랜덤 닉네임 생성 기능 구현 (#65)
1 parent 591c23d commit d68a20e

File tree

5 files changed

+255
-2
lines changed

5 files changed

+255
-2
lines changed

src/main/java/com/moa/moa_server/domain/auth/service/strategy/KakaoOAuthLoginStrategy.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.moa.moa_server.domain.auth.service.RefreshTokenService;
1111
import com.moa.moa_server.domain.user.entity.User;
1212
import com.moa.moa_server.domain.user.repository.UserRepository;
13+
import com.moa.moa_server.domain.user.util.NicknameGenerator;
1314
import lombok.RequiredArgsConstructor;
1415
import lombok.extern.slf4j.Slf4j;
1516
import org.springframework.beans.factory.annotation.Value;
@@ -72,8 +73,9 @@ public LoginResult login(String code) {
7273
.map(OAuth::getUser)
7374
.orElseGet(() -> {
7475
// 신규 회원가입
76+
String nickname = NicknameGenerator.generate(userRepository);
7577
User newUser = User.builder()
76-
.nickname("kakao_" + kakaoId)
78+
.nickname(nickname)
7779
.role(User.Role.USER)
7880
.userStatus(User.UserStatus.ACTIVE)
7981
.lastActiveAt(LocalDateTime.now())

src/main/java/com/moa/moa_server/domain/user/handler/UserErrorCode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ public enum UserErrorCode implements BaseErrorCode {
99
USER_WITHDRAWN(HttpStatus.UNAUTHORIZED),
1010
INVALID_CURSOR_FORMAT(HttpStatus.BAD_REQUEST),
1111
INVALID_NICKNAME(HttpStatus.BAD_REQUEST),
12-
DUPLICATED_NICKNAME(HttpStatus.CONFLICT);
12+
DUPLICATED_NICKNAME(HttpStatus.CONFLICT),
13+
NICKNAME_GENERATION_FAILED(HttpStatus.INTERNAL_SERVER_ERROR);
1314

1415
private final HttpStatus status;
1516

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.moa.moa_server.domain.user.util;
2+
3+
import com.moa.moa_server.domain.user.handler.UserErrorCode;
4+
import com.moa.moa_server.domain.user.handler.UserException;
5+
import com.moa.moa_server.domain.user.repository.UserRepository;
6+
7+
import java.io.BufferedReader;
8+
import java.io.InputStream;
9+
import java.io.InputStreamReader;
10+
import java.nio.charset.StandardCharsets;
11+
import java.util.List;
12+
import java.util.Random;
13+
import java.util.stream.Collectors;
14+
15+
public class NicknameGenerator {
16+
17+
private static final List<String> ADJECTIVES = loadWords("nickname/adjectives.txt");
18+
private static final List<String> NOUNS = loadWords("nickname/nouns.txt");
19+
20+
private static final Random RANDOM = new Random();
21+
22+
public static String generate(UserRepository userRepository) {
23+
for (int i = 0; i < 20; i++) { // 중복 시 최대 20번 재시도
24+
String adj = ADJECTIVES.get(RANDOM.nextInt(ADJECTIVES.size()));
25+
String noun = NOUNS.get(RANDOM.nextInt(NOUNS.size()));
26+
String nickname = adj + noun;
27+
28+
if (nickname.length() >= 3 && nickname.length() <= 10 && !userRepository.existsByNickname(nickname)) {
29+
return nickname;
30+
}
31+
}
32+
throw new UserException(UserErrorCode.NICKNAME_GENERATION_FAILED);
33+
}
34+
35+
private static List<String> loadWords(String path) {
36+
try (InputStream inputStream = NicknameGenerator.class.getClassLoader().getResourceAsStream(path)) {
37+
if (inputStream == null) {
38+
throw new IllegalStateException("Resource not found: " + path);
39+
}
40+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
41+
return reader.lines()
42+
.map(String::trim)
43+
.filter(line -> !line.isEmpty())
44+
.collect(Collectors.toList());
45+
}
46+
} catch (Exception e) {
47+
throw new RuntimeException("Failed to load word list from: " + path, e);
48+
}
49+
}
50+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
반짝이는
2+
상큼한
3+
몽글몽글한
4+
따뜻한
5+
포근한
6+
귀여운
7+
엉뚱한
8+
싱그러운
9+
졸린
10+
즐거운
11+
말랑말랑한
12+
느긋한
13+
활기찬
14+
배고픈
15+
졸린
16+
살랑살랑한
17+
시원한
18+
부드러운
19+
상냥한
20+
짹짹이는
21+
개구진
22+
웃고있는
23+
조용한
24+
신비한
25+
설레는
26+
기분좋은
27+
예쁜
28+
멋진
29+
도도한
30+
느끼한
31+
말쑥한
32+
순수한
33+
요상한
34+
엉성한
35+
단단한
36+
촉촉한
37+
말많은
38+
수줍은
39+
덜렁이는
40+
우아한
41+
예리한
42+
어리둥절한
43+
기발한
44+
새침한
45+
씩씩한
46+
덤덤한
47+
깔끔한
48+
비밀스러운
49+
은은한
50+
따스한
51+
비오는
52+
눈오는
53+
햇살가득한
54+
무지개빛
55+
달콤한
56+
쌉싸름한
57+
촉촉한
58+
반가운
59+
산뜻한
60+
아련한
61+
몽환적인
62+
자상한
63+
느끼한
64+
개운한
65+
고요한
66+
활짝핀
67+
햇살좋은
68+
여유로운
69+
도톰한
70+
쫀득한
71+
푸짐한
72+
알찬
73+
낙엽진
74+
바람부는
75+
잎새진
76+
봄내음나는
77+
가을타는
78+
겨울잠자는
79+
여름같은
80+
상큼발랄한
81+
토실토실한
82+
폭신한
83+
꿀떨어지는
84+
촉촉한
85+
은근한
86+
한적한
87+
화사한
88+
명랑한
89+
엉뚱발랄한
90+
사랑스러운
91+
달달한
92+
반가운
93+
두근두근한
94+
바삭한
95+
탱글탱글한
96+
폭풍같은
97+
행복한
98+
씩씩한
99+
단순한
100+
느린
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
두더지
2+
사자
3+
꿀떡
4+
푸딩
5+
펭귄
6+
토끼
7+
솜사탕
8+
사과
9+
청포도
10+
찐빵
11+
호랑이
12+
샌드위치
13+
젤리곰
14+
젤리
15+
시나몬롤
16+
버터
17+
악어
18+
헤지호그
19+
펄럭이
20+
망고
21+
복숭아
22+
너구리
23+
카라멜
24+
마카롱
25+
도넛
26+
냥이
27+
사이다
28+
딸기
29+
크루아상
30+
바게트
31+
초코칩
32+
부엉이
33+
캔디
34+
푸바오
35+
햄버거
36+
푸딩곰
37+
와플
38+
앵무새
39+
바나나
40+
문어빵
41+
젤라또
42+
하마
43+
팝콘
44+
빙수
45+
염소
46+
초코
47+
콜라
48+
49+
토끼곰
50+
51+
아기고양이
52+
마시멜로
53+
수박
54+
라즈베리
55+
호빵
56+
타르트
57+
푸들
58+
여우
59+
고래
60+
무화과
61+
식빵
62+
치즈볼
63+
블루베리
64+
멍멍이
65+
66+
병아리
67+
쿠키
68+
떡볶이
69+
곰돌이
70+
체리
71+
롤케이크
72+
아이스크림
73+
카스테라
74+
젤리빈
75+
스폰지밥
76+
코끼리
77+
우유
78+
파인애플
79+
햄스터
80+
카라멜콘
81+
다람쥐
82+
문어
83+
강아지
84+
짱구
85+
고양이
86+
브라우니
87+
상어
88+
베이글
89+
하트냥
90+
소보로빵
91+
92+
치와와
93+
94+
치즈
95+
머랭
96+
딸기곰
97+
당고
98+
딸기우유
99+
붕어빵
100+

0 commit comments

Comments
 (0)