-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : 메일을 전송하고, 인증 코드를 확인하는 UnivEmailVerificationService 구현 (#28)
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
src/main/java/com/gdschongik/gdsc/domain/integration/UnivEmailVerificationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |