diff --git a/src/main/java/cotato/growingpain/auth/controller/AuthController.java b/src/main/java/cotato/growingpain/auth/controller/AuthController.java index 6796b7c..30e265f 100644 --- a/src/main/java/cotato/growingpain/auth/controller/AuthController.java +++ b/src/main/java/cotato/growingpain/auth/controller/AuthController.java @@ -2,11 +2,14 @@ import cotato.growingpain.auth.dto.request.ChangePasswordRequest; import cotato.growingpain.auth.dto.request.CompleteSignupRequest; +import cotato.growingpain.auth.dto.request.DuplicateCheckRequest; import cotato.growingpain.auth.dto.request.LoginRequest; import cotato.growingpain.auth.dto.request.LogoutRequest; import cotato.growingpain.auth.dto.request.ResetPasswordRequest; +import cotato.growingpain.auth.dto.response.DuplicateCheckResponse; import cotato.growingpain.auth.dto.response.ResetPasswordResponse; import cotato.growingpain.auth.service.AuthService; +import cotato.growingpain.auth.service.ValidateService; import cotato.growingpain.common.Response; import cotato.growingpain.security.jwt.Token; import cotato.growingpain.security.jwt.dto.request.ReissueRequest; @@ -23,6 +26,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; @@ -38,6 +42,7 @@ public class AuthController { private final AuthService authService; + private final ValidateService validateService; @Operation(summary = "일반 로그인", description = "회원가입 및 로그인을 위한 메소드") @ApiResponse(content = @Content(schema = @Schema(implementation = Token.class))) @@ -94,4 +99,34 @@ public Response changePassword(@RequestBody @Valid ChangePasswordRequest requ authService.changePassword(request, memberId); return Response.createSuccessWithNoData("비밀번호 변경 완료"); } + + @Operation(summary = "이메일 중복 검증", description = "회원가입 시 기존 사용자와 이메일이 중복되는지 확인하는 메소드") + @ApiResponse(content = @Content(schema = @Schema(implementation = DuplicateCheckResponse.class))) + @GetMapping("/validate/email") + @ResponseStatus(HttpStatus.OK) + public Response checkDuplicateEmail(@RequestBody DuplicateCheckRequest request) { + + boolean isDuplicate = validateService.isDuplicateEmail(request.value()); + DuplicateCheckResponse response = new DuplicateCheckResponse(isDuplicate); + if (isDuplicate) { + return Response.createSuccess("이미 사용 중인 이메일입니다.", response); + } else { + return Response.createSuccess("사용 가능한 이메일입니다.", response); + } + } + + @Operation(summary = "닉네임 중복 검증", description = "추가 정보 입력 시 기존 사용자와 닉네임이 중복되는지 확인하는 메소드") + @ApiResponse(content = @Content(schema = @Schema(implementation = DuplicateCheckResponse.class))) + @GetMapping("/validate/nickname") + @ResponseStatus(HttpStatus.OK) + public Response checkDuplicateNickname(@RequestBody DuplicateCheckRequest request) { + boolean isDuplicate = validateService.isDuplicateNickname(request.value()); + DuplicateCheckResponse response = new DuplicateCheckResponse(isDuplicate); + + if (isDuplicate) { + return Response.createSuccess("이미 사용 중인 닉네임입니다.", response); + } else { + return Response.createSuccess("사용 가능한 닉네임입니다.", response); + } + } } \ No newline at end of file diff --git a/src/main/java/cotato/growingpain/auth/dto/request/DuplicateCheckRequest.java b/src/main/java/cotato/growingpain/auth/dto/request/DuplicateCheckRequest.java new file mode 100644 index 0000000..b7f60a8 --- /dev/null +++ b/src/main/java/cotato/growingpain/auth/dto/request/DuplicateCheckRequest.java @@ -0,0 +1,6 @@ +package cotato.growingpain.auth.dto.request; + +public record DuplicateCheckRequest( + String value +) { +} diff --git a/src/main/java/cotato/growingpain/auth/dto/response/DuplicateCheckResponse.java b/src/main/java/cotato/growingpain/auth/dto/response/DuplicateCheckResponse.java new file mode 100644 index 0000000..e5ac509 --- /dev/null +++ b/src/main/java/cotato/growingpain/auth/dto/response/DuplicateCheckResponse.java @@ -0,0 +1,6 @@ +package cotato.growingpain.auth.dto.response; + +public record DuplicateCheckResponse( + Boolean isDuplicate +) { +} diff --git a/src/main/java/cotato/growingpain/auth/service/ValidateService.java b/src/main/java/cotato/growingpain/auth/service/ValidateService.java index af7d85e..9004572 100644 --- a/src/main/java/cotato/growingpain/auth/service/ValidateService.java +++ b/src/main/java/cotato/growingpain/auth/service/ValidateService.java @@ -18,7 +18,6 @@ public class ValidateService { private static final String PASSWORD_PATTERN = "^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,16}$"; private final MemberRepository memberRepository; - public void checkPasswordPattern(String password) { Pattern pattern = Pattern.compile(PASSWORD_PATTERN); Matcher matcher = pattern.matcher(password); @@ -37,16 +36,24 @@ public boolean isValidPassword(String password) { } public void checkDuplicateNickName(String name) { - if (memberRepository.findByName(name).isPresent()) { + if (isDuplicateNickname(name)) { log.error("[회원 가입 실패]: 존재하는 닉네임 " + name); throw new AppException(ErrorCode.NICKNAME_DUPLICATED); } } public void checkDuplicateEmail(@NotBlank(message = "로그인 할 아이디를 입력해주세요") String email) { - if (memberRepository.findByEmail(email).isPresent()) { + if (isDuplicateEmail(email)) { log.error("[회원 가입 실패]: 중복된 이메일 " + email); throw new AppException(ErrorCode.EMAIL_DUPLICATED); } } + + public boolean isDuplicateEmail(String email) { + return memberRepository.findByEmail(email).isPresent(); + } + + public boolean isDuplicateNickname(String nickName) { + return memberRepository.findByName(nickName).isPresent(); + } } \ No newline at end of file