Skip to content

Commit

Permalink
feat : 메일을 전송하고, 인증 코드를 확인하는 UnivEmailVerificationService 구현 (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
binary-ho committed Feb 11, 2024
1 parent 3f94458 commit bc80e49
Showing 1 changed file with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.gdschongik.gdsc.domain.integration;

import com.gdschongik.gdsc.global.exception.CustomException;
import com.gdschongik.gdsc.global.exception.ErrorCode;
import java.time.Duration;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class UnivEmailVerificationService {

private final HongikUnivEmailValidator hongikUnivEmailValidator;
private final VerificationCodeGenerator verificationCodeGenerator;
private final VerificationMailContentWriter verificationMailContentWriter;

private final MailSender mailSender;
private final EmailVerificationCodeRepository emailVerificationCodeRepository;

public static final Duration VERIFICATION_CODE_TIME_TO_LIVE = Duration.ofMinutes(10);

public void sendEmailVerificationMail(String email) {
hongikUnivEmailValidator.validate(email);

String verificationCode = verificationCodeGenerator.generate();
sendMail(email, verificationCode);

saveVerificationCode(email, verificationCode);
}

private void sendMail(String email, String verificationCode) {
String mailContent = verificationMailContentWriter
.writeContentWithVerificationCode(verificationCode, VERIFICATION_CODE_TIME_TO_LIVE);

mailSender.send(email, mailContent);
}

private void saveVerificationCode(String email, String verificationCode) {
EmailVerificationCode emailVerificationCode = new EmailVerificationCode(
email, verificationCode, VERIFICATION_CODE_TIME_TO_LIVE.toSeconds()
);

emailVerificationCodeRepository.save(emailVerificationCode);
}

public void validateCodeMatch(String email, String userInputCode) {
String verificationCode = getVerificationCodeByEmail(email);

if (!Objects.equals(verificationCode, userInputCode)) {
throw new CustomException(ErrorCode.UNIV_EMAIL_VERIFICATION_CODE_NOT_MATCH);
}
}

private String getVerificationCodeByEmail(String email) {
return emailVerificationCodeRepository.findById(email)
.orElseThrow(() -> new CustomException(ErrorCode.VERIFICATION_CODE_NOT_FOUND))
.getVerificationCode();
}
}

0 comments on commit bc80e49

Please sign in to comment.