From d30135e1c78da5345e8b343a96b8a83c0dba8069 Mon Sep 17 00:00:00 2001 From: ywonchae1 Date: Thu, 31 Oct 2024 13:31:19 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=9D=B4=EB=A9=94=EC=9D=BC=20=EC=9D=B8?= =?UTF-8?q?=EC=A6=9D=20=EC=BD=94=EB=93=9C=20=EA=B2=80=EC=A6=9D=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20(#54)=20(KAN-106)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../meonghanyangserver/auth/controller/AuthApi.java | 6 +++++- .../auth/controller/AuthController.java | 9 ++++++++- .../auth/dto/request/VerifyEmailRequest.java | 9 +++++++++ .../auth/service/AuthService.java | 13 +++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/ioteatime/meonghanyangserver/auth/dto/request/VerifyEmailRequest.java diff --git a/src/main/java/org/ioteatime/meonghanyangserver/auth/controller/AuthApi.java b/src/main/java/org/ioteatime/meonghanyangserver/auth/controller/AuthApi.java index 05a964f8..ae7e3476 100644 --- a/src/main/java/org/ioteatime/meonghanyangserver/auth/controller/AuthApi.java +++ b/src/main/java/org/ioteatime/meonghanyangserver/auth/controller/AuthApi.java @@ -7,6 +7,7 @@ import org.ioteatime.meonghanyangserver.auth.dto.reponse.RefreshResponse; import org.ioteatime.meonghanyangserver.auth.dto.request.EmailRequest; import org.ioteatime.meonghanyangserver.auth.dto.request.LoginRequest; +import org.ioteatime.meonghanyangserver.auth.dto.request.VerifyEmailRequest; import org.ioteatime.meonghanyangserver.common.api.Api; import org.ioteatime.meonghanyangserver.user.dto.UserDto; import org.ioteatime.meonghanyangserver.user.dto.response.UserSimpleResponse; @@ -19,7 +20,10 @@ public interface AuthApi { Api registerUser(@Valid @RequestBody UserDto userDto); @Operation(summary = "인증 메일 전송") - Api verifyEmail(@Valid @RequestBody EmailRequest email); + Api sendEmailCode(@Valid @RequestBody EmailRequest email); + + @Operation(summary = "메일 인증 코드 검증") + Api verifyEmail(@Valid @RequestBody VerifyEmailRequest verifyEmailRequest); @Operation(summary = "로그인을 합니다.") Api login(@RequestBody @Valid LoginRequest loginRequest); diff --git a/src/main/java/org/ioteatime/meonghanyangserver/auth/controller/AuthController.java b/src/main/java/org/ioteatime/meonghanyangserver/auth/controller/AuthController.java index 91c8a963..f3d1a6be 100644 --- a/src/main/java/org/ioteatime/meonghanyangserver/auth/controller/AuthController.java +++ b/src/main/java/org/ioteatime/meonghanyangserver/auth/controller/AuthController.java @@ -6,6 +6,7 @@ import org.ioteatime.meonghanyangserver.auth.dto.reponse.RefreshResponse; import org.ioteatime.meonghanyangserver.auth.dto.request.EmailRequest; import org.ioteatime.meonghanyangserver.auth.dto.request.LoginRequest; +import org.ioteatime.meonghanyangserver.auth.dto.request.VerifyEmailRequest; import org.ioteatime.meonghanyangserver.auth.service.AuthService; import org.ioteatime.meonghanyangserver.common.api.Api; import org.ioteatime.meonghanyangserver.user.dto.UserDto; @@ -25,11 +26,17 @@ public Api registerUser(@Valid @RequestBody UserDto userDto) { } @PostMapping("/email-verification") - public Api verifyEmail(@Valid @RequestBody EmailRequest emailReq) { + public Api sendEmailCode(@Valid @RequestBody EmailRequest emailReq) { authService.send(emailReq.email()); return Api.OK(); } + @PostMapping("/check-verification") + public Api verifyEmail(VerifyEmailRequest verifyEmailRequest) { + authService.verifyEmailCode(verifyEmailRequest.email(), verifyEmailRequest.code()); + return Api.OK(); + } + // Email 중복 확인 @PostMapping("/check-email") public Api duplicateEmail(@Valid @RequestBody EmailRequest emailReq) { diff --git a/src/main/java/org/ioteatime/meonghanyangserver/auth/dto/request/VerifyEmailRequest.java b/src/main/java/org/ioteatime/meonghanyangserver/auth/dto/request/VerifyEmailRequest.java new file mode 100644 index 00000000..e5179752 --- /dev/null +++ b/src/main/java/org/ioteatime/meonghanyangserver/auth/dto/request/VerifyEmailRequest.java @@ -0,0 +1,9 @@ +package org.ioteatime.meonghanyangserver.auth.dto.request; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.Email; +import jakarta.validation.constraints.NotNull; + +public record VerifyEmailRequest( + @Email @NotNull @Schema(description = "이메일", example = "example@gmail.com") String email, + @NotNull @Schema(description = "인증 코드", example = "XV23W1") String code) {} diff --git a/src/main/java/org/ioteatime/meonghanyangserver/auth/service/AuthService.java b/src/main/java/org/ioteatime/meonghanyangserver/auth/service/AuthService.java index 3d590345..7e91d667 100644 --- a/src/main/java/org/ioteatime/meonghanyangserver/auth/service/AuthService.java +++ b/src/main/java/org/ioteatime/meonghanyangserver/auth/service/AuthService.java @@ -110,6 +110,19 @@ public UserSimpleResponse verifyEmail(String email) { return AuthResponseMapper.from(userEntity.getId(), userEntity.getEmail()); } + public void verifyEmailCode(String email, String code) { + EmailCode emailCode = + emailCodeRepository + .findByEmail(email) + .orElseThrow( + () -> + new ApiExceptionImpl( + ErrorTypeCode.NULL_POINT, "Code not found")); + if (!code.equals(emailCode.getCode())) { + throw new ApiExceptionImpl(ErrorTypeCode.NULL_POINT, "Code not equals"); + } + } + public RefreshResponse reissueAccessToken(String authorizationHeader) { String refreshToken = jwtUtils.extractTokenFromHeader(authorizationHeader);