Skip to content

Commit

Permalink
[#9] feat : 인증번호 확인 기능구현 및 회원가입 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
princenim committed Mar 29, 2024
1 parent 2102304 commit 1457f8e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.kboticketing.kboticketing.dto.EmailRequestDto;
import com.kboticketing.kboticketing.dto.UserDto;
import com.kboticketing.kboticketing.dto.VerificationCodeDto;
import com.kboticketing.kboticketing.service.UserService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand All @@ -27,5 +28,10 @@ public void signUp(@RequestBody @Valid UserDto userDto) {
public void sendVerificationCode(@RequestBody @Valid EmailRequestDto emailRequestDto) {
userService.sendVerificationCode(emailRequestDto);
}

@PostMapping("verification-code-validation")
public void checkVerificationCode(@RequestBody @Valid VerificationCodeDto verificationCodeDto) {
userService.checkVerificationCode(verificationCodeDto);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.kboticketing.kboticketing.dto;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* @author hazel
*/
@Getter
@RequiredArgsConstructor
public class VerificationCodeDto {

@NotBlank(message = "이메일을 입력해주세요.")
@Email(message = "이메일 형식이 아닙니다.")
private final String email;

@NotBlank(message = "인증번호를 입력해주세요.")
private final String verificationCode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import com.kboticketing.kboticketing.domain.User;
import com.kboticketing.kboticketing.dto.EmailRequestDto;
import com.kboticketing.kboticketing.dto.UserDto;
import com.kboticketing.kboticketing.dto.VerificationCodeDto;
import com.kboticketing.kboticketing.utils.EmailUtils;
import com.kboticketing.kboticketing.utils.exception.CustomException;
import com.kboticketing.kboticketing.utils.exception.ErrorCode;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -31,7 +33,10 @@ public class UserService {
private final RedisTemplate<String, String> redisTemplate;

public void signUp(UserDto userDto) {
//todo 인증번호 확인 로직 -> 인증번호 API 작업 후 작업 예정
Boolean hasKey = redisTemplate.hasKey(userDto.getEmail());
if (Boolean.FALSE.equals(hasKey)) {
throw new CustomException(ErrorCode.REQUEST_VERIFICATION_FIRST);
}

if (!userDto.getPassword()
.equals(userDto.getConfirmedPassword())) {
Expand Down Expand Up @@ -62,6 +67,19 @@ public void sendVerificationCode(EmailRequestDto emailRequestDto) {

emailUtil.sendEmail(verificationCode, emailRequestDto.getEmail());
}

public void checkVerificationCode(VerificationCodeDto verificationCodeDto) {
Boolean hasKey = redisTemplate.hasKey(verificationCodeDto.getEmail());
if (Boolean.FALSE.equals(hasKey)) {
throw new CustomException(ErrorCode.REQUEST_VERIFICATION_FIRST);
}

String storedVerificationCode = redisTemplate.opsForValue()
.get(verificationCodeDto.getEmail());
if (!Objects.equals(verificationCodeDto.getVerificationCode(), storedVerificationCode)) {
throw new CustomException(ErrorCode.WRONG_VERIFICATION_CODE);
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ public enum ErrorCode {
PASSWORD_MISMATCH(HttpStatus.BAD_REQUEST, "두 비밀번호가 일치하지 않습니다."),
EMAIL_ALREADY_EXISTS(HttpStatus.BAD_REQUEST, "이미 존재하는 이메일입니다."),
FREQUENT_VERIFICATION_REQUEST(HttpStatus.BAD_REQUEST, "잦은 인증번호 요청은 불가능합니다."),
REQUEST_VERIFICATION_FIRST(HttpStatus.BAD_REQUEST, "인증을 먼저 진행해주세요."),
WRONG_VERIFICATION_CODE(HttpStatus.BAD_REQUEST, "인증번호가 다릅니다."),

/* 500 INTERNAL_SERVER_ERROR : 서버 오류 */
FAIL_SEND_EMAIL(HttpStatus.INTERNAL_SERVER_ERROR, "이메일 전송에 실패하였습니다.");

private final HttpStatus httpStatus;
private final String message;
}
}

0 comments on commit 1457f8e

Please sign in to comment.