Skip to content

Commit db320ee

Browse files
committed
Feat: 비밀번호 재설정 API 구현
1 parent 9a241fd commit db320ee

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

src/main/java/com/back/domain/user/controller/AuthController.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,17 @@ public ResponseEntity<RsData<Void>> recoverPassword(
125125
null
126126
));
127127
}
128+
129+
// 비밀번호 재설정
130+
@PostMapping("/password/reset")
131+
public ResponseEntity<RsData<Void>> resetPassword(
132+
@Valid @RequestBody PasswordResetRequest request
133+
) {
134+
authService.resetPassword(request.token(), request.newPassword());
135+
return ResponseEntity
136+
.ok(RsData.success(
137+
"비밀번호가 성공적으로 재설정되었습니다.",
138+
null
139+
));
140+
}
128141
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.back.domain.user.dto;
2+
3+
import jakarta.validation.constraints.NotBlank;
4+
5+
/**
6+
* 비밀번호 재설정 요청 DTO
7+
*
8+
* @param token 비밀번호 재설정 토큰
9+
* @param newPassword 새 비밀번호
10+
*/
11+
public record PasswordResetRequest(
12+
@NotBlank String token,
13+
@NotBlank String newPassword
14+
) {
15+
}

src/main/java/com/back/domain/user/service/AuthService.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,40 @@ public void recoverPassword(String email) {
303303
emailService.sendPasswordResetEmail(user.getEmail(), resetToken);
304304
}
305305

306+
/**
307+
* 비밀번호 재설정 서비스
308+
* 1. 토큰 검증
309+
* 2. 사용자 조회
310+
* 3. 비밀번호 정책 검증
311+
* 4. 비밀번호 변경
312+
* 5. 토큰 삭제
313+
*/
314+
public void resetPassword(String token, String newPassword) {
315+
// 토큰 존재 여부 확인
316+
if (token == null || token.isEmpty()) {
317+
throw new CustomException(ErrorCode.BAD_REQUEST);
318+
}
319+
320+
// 토큰으로 사용자 ID 조회
321+
Long userId = tokenService.getUserIdByPasswordResetToken(token);
322+
if (userId == null) {
323+
throw new CustomException(ErrorCode.INVALID_PASSWORD_RESET_TOKEN);
324+
}
325+
326+
// 사용자 조회
327+
User user = userRepository.findById(userId)
328+
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
329+
330+
// 비밀번호 정책 검증
331+
PasswordValidator.validate(newPassword);
332+
333+
// 비밀번호 변경
334+
user.setPassword(passwordEncoder.encode(newPassword));
335+
336+
// 토큰 삭제 (재사용 방지)
337+
tokenService.deletePasswordResetToken(token);
338+
}
339+
306340
/**
307341
* 회원가입 시 중복 검증
308342
* - username, email, nickname

src/main/java/com/back/global/exception/ErrorCode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ public enum ErrorCode {
9797

9898
// ======================== 토큰 관련 ========================
9999
INVALID_EMAIL_TOKEN(HttpStatus.UNAUTHORIZED, "TOKEN_001", "유효하지 않은 이메일 인증 토큰입니다."),
100-
ALREADY_VERIFIED(HttpStatus.CONFLICT, "TOKEN_002", "이미 인증된 계정입니다.");
100+
ALREADY_VERIFIED(HttpStatus.CONFLICT, "TOKEN_002", "이미 인증된 계정입니다."),
101+
INVALID_PASSWORD_RESET_TOKEN(HttpStatus.UNAUTHORIZED, "TOKEN_003", "유효하지 않은 비밀번호 재설정 토큰입니다.");
101102

102103
private final HttpStatus status;
103104
private final String code;

0 commit comments

Comments
 (0)