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/EmailRequest.java b/src/main/java/org/ioteatime/meonghanyangserver/auth/dto/request/EmailRequest.java index 39d8c8a8..25873ab9 100644 --- a/src/main/java/org/ioteatime/meonghanyangserver/auth/dto/request/EmailRequest.java +++ b/src/main/java/org/ioteatime/meonghanyangserver/auth/dto/request/EmailRequest.java @@ -1,10 +1,8 @@ package org.ioteatime.meonghanyangserver.auth.dto.request; import io.swagger.v3.oas.annotations.media.Schema; -import jakarta.validation.Valid; import jakarta.validation.constraints.Email; import jakarta.validation.constraints.NotNull; public record EmailRequest( - @Valid @Email @NotNull @Schema(description = "이메일", example = "example@gmail.com") - String email) {} + @Email @NotNull @Schema(description = "이메일", example = "example@gmail.com") String email) {} 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);