From 765648095f875a6e015fb625a36661aff6a586d9 Mon Sep 17 00:00:00 2001 From: yunhacandy Date: Thu, 15 Aug 2024 16:27:40 +0900 Subject: [PATCH 1/3] =?UTF-8?q?refactor:=20=EC=97=90=EB=9F=AC=20=EB=A9=94?= =?UTF-8?q?=EC=8B=9C=EC=A7=80=20=EC=A2=85=EB=A5=98=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cotato/growingpain/auth/service/AuthService.java | 5 ++--- .../java/cotato/growingpain/common/exception/ErrorCode.java | 3 ++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/cotato/growingpain/auth/service/AuthService.java b/src/main/java/cotato/growingpain/auth/service/AuthService.java index 173c228..3c3fa0e 100644 --- a/src/main/java/cotato/growingpain/auth/service/AuthService.java +++ b/src/main/java/cotato/growingpain/auth/service/AuthService.java @@ -51,7 +51,7 @@ public Token createLoginInfo(AuthProvider authProvider, LoginRequest request) { // 기존 회원이 존재하면 로그인 처리 Member member = existingMember.get(); if (!bCryptPasswordEncoder.matches(request.password(), member.getPassword())) { - throw new AppException(ErrorCode.INVALID_PASSWORD); + throw new AppException(ErrorCode.WRONG_PASSWORD); } String role = (member.getMemberRole() == MemberRole.PENDING) @@ -122,7 +122,6 @@ public Token completeSignup(CompleteSignupRequest request, String accessToken) { return token; } - //validateService.checkDuplicateNickName(request.name()); TODO log.info("memberRole = {}", member.getMemberRole()); return null; } @@ -218,7 +217,7 @@ public void changePassword(ChangePasswordRequest request, Long memberId) { // 기존 비밀번호 검증 if (!bCryptPasswordEncoder.matches(request.currentPassword(), member.getPassword())) { - throw new AppException(ErrorCode.INVALID_PASSWORD); + throw new AppException(ErrorCode.WRONG_PASSWORD); } // 새 비밀번호 패턴 검증 diff --git a/src/main/java/cotato/growingpain/common/exception/ErrorCode.java b/src/main/java/cotato/growingpain/common/exception/ErrorCode.java index d175c1c..cd338c5 100644 --- a/src/main/java/cotato/growingpain/common/exception/ErrorCode.java +++ b/src/main/java/cotato/growingpain/common/exception/ErrorCode.java @@ -11,7 +11,8 @@ public enum ErrorCode { //회원가입 EMAIL_DUPLICATED(HttpStatus.CONFLICT, "존재하는 이메일입니다."), NICKNAME_DUPLICATED(HttpStatus.CONFLICT, "존재하는 닉네임입니다."), - INVALID_PASSWORD(HttpStatus.BAD_REQUEST, "유효하지 않은 패스워드입니다."), + INVALID_PASSWORD(HttpStatus.BAD_REQUEST, "유효하지 않은 비밀번호 형식입니다."), + WRONG_PASSWORD(HttpStatus.BAD_REQUEST, "비밀번호가 틀렸습니다."), ACCESS_DENIED_USER(HttpStatus.FORBIDDEN,"권한이 없는 유저입니다."), EMAIL_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 이메일이 존재하지 않습니다."), From a42b5e55106bc1785ea914ea449f4a2c6c49ed90 Mon Sep 17 00:00:00 2001 From: yunhacandy Date: Thu, 15 Aug 2024 16:29:36 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=EB=8B=89=EB=84=A4=EC=9E=84=20?= =?UTF-8?q?=EC=A4=91=EB=B3=B5=20=EB=B0=A9=EC=A7=80=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/cotato/growingpain/auth/service/AuthService.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/cotato/growingpain/auth/service/AuthService.java b/src/main/java/cotato/growingpain/auth/service/AuthService.java index 3c3fa0e..74ec9c3 100644 --- a/src/main/java/cotato/growingpain/auth/service/AuthService.java +++ b/src/main/java/cotato/growingpain/auth/service/AuthService.java @@ -108,6 +108,9 @@ public Token completeSignup(CompleteSignupRequest request, String accessToken) { log.info("추가 정보 입력 받는 이메일: {}", email); if(member.getMemberRole() == MemberRole.PENDING){ + + validateService.checkDuplicateNickName(request.name()); + // 필드를 개별적으로 업데이트 member.updateMemberInfo(request.name(), request.field(), request.belong(),request.job()); member.updateRole(MemberRole.MEMBER); From ed4b56e5f8007be603e23751432a30ae98a53e4e Mon Sep 17 00:00:00 2001 From: yunhacandy Date: Thu, 15 Aug 2024 19:21:31 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=EB=8B=89=EB=84=A4=EC=9E=84=20?= =?UTF-8?q?=EB=B0=8F=20=EC=9D=B4=EB=A9=94=EC=9D=BC=20=EC=A4=91=EB=B3=B5=20?= =?UTF-8?q?=EB=B0=A9=EC=A7=80=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/controller/AuthController.java | 35 +++++++++++++++++++ .../dto/request/DuplicateCheckRequest.java | 6 ++++ .../dto/response/DuplicateCheckResponse.java | 6 ++++ .../auth/service/ValidateService.java | 13 +++++-- 4 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 src/main/java/cotato/growingpain/auth/dto/request/DuplicateCheckRequest.java create mode 100644 src/main/java/cotato/growingpain/auth/dto/response/DuplicateCheckResponse.java 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