-
Notifications
You must be signed in to change notification settings - Fork 1
[REFACTOR] 메일 도메인 리팩토링 #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
paragon0107
merged 23 commits into
develop
from
chore/#147-auth-member-mail-team-refactor
Oct 4, 2024
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
a5d72c6
[CHORE] val 삭제
paragon0107 8e3e745
[CHORE] email 원시값 포장
paragon0107 d89d5c3
[CHORE] 널 체크 추가
paragon0107 cdd758b
[CHORE] 인증 코드 확인 dto 이름 수정
paragon0107 e7b44ef
[CHORE] email 용어 통일
paragon0107 3a0ab7c
[CHORE] 기존 mail 도메인 -> EmailVerification 도메인으로 수정
paragon0107 4904e64
[CHORE] 기존 mail 도메인 삭제
paragon0107 c06cf13
[CHORE] 메일 Send 기능 분리
paragon0107 fe65ee2
[CHORE] 쓸모없는 import문 삭제
paragon0107 56b6f25
[CHORE] 컨베션 적용
paragon0107 c6188ed
[CHORE] DTO 스트링으로 변경
paragon0107 b81b52d
[CHORE] 이메일 검증 로직 수정
paragon0107 469bc8c
[CHORE] 코드 검증 메서드 위치 변경
paragon0107 6a493be
[CHORE] 코드 검증 로직 수정
paragon0107 36e071d
[CHORE] 메서드 제거
paragon0107 9bd20f8
[CHORE] EmailValidator 관련리 라이브러리 추가
paragon0107 7653741
[CHORE] EmailValidator로 형식 증명
paragon0107 43681c3
[CHORE] 컨벤션 적용
paragon0107 e5630e9
[CHORE] 패키지명 변경
paragon0107 1ca1db8
[CHORE] 패키지명 변경
paragon0107 dbf9cf6
[CHORE] 패키지명 변경
paragon0107 420c273
[CHORE] 허용 API 수정
paragon0107 663e4d2
[CHORE] INIT_NUM 상수화
paragon0107 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,29 @@ | ||
| package com.tiki.server.common.entity; | ||
|
|
||
| import com.tiki.server.member.exception.MemberException; | ||
| import jakarta.persistence.Embeddable; | ||
| import lombok.*; | ||
| import org.apache.commons.validator.routines.EmailValidator; | ||
|
|
||
| import static com.tiki.server.emailverification.constants.EmailConstants.MAIL_FORMAT_AC_KR; | ||
| import static com.tiki.server.emailverification.constants.EmailConstants.MAIL_FORMAT_EDU; | ||
| import static com.tiki.server.member.message.ErrorCode.INVALID_EMAIL; | ||
|
|
||
| @Getter | ||
| @Embeddable | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| @AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
| public class Email { | ||
|
|
||
| private String email; | ||
Chan531 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public static Email from(String email){ | ||
| checkMailFormat(email); | ||
| return new Email(email); | ||
| } | ||
|
|
||
| private static void checkMailFormat(String email) { | ||
Chan531 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!EmailValidator.getInstance().isValid(email) || !(email.endsWith(MAIL_FORMAT_EDU) || email.endsWith(MAIL_FORMAT_AC_KR))) { | ||
| throw new MemberException(INVALID_EMAIL); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
20 changes: 20 additions & 0 deletions
20
src/main/java/com/tiki/server/emailverification/adapter/EmailVerificationFinder.java
This file contains hidden or 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,20 @@ | ||
| package com.tiki.server.emailverification.adapter; | ||
|
|
||
| import com.tiki.server.common.support.RepositoryAdapter; | ||
| import com.tiki.server.emailverification.domain.EmailVerification; | ||
| import com.tiki.server.emailverification.exception.EmailVerificationException; | ||
| import com.tiki.server.emailverification.repository.EmailVerificationRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| import static com.tiki.server.emailverification.message.ErrorCode.INVALID_REQUEST; | ||
|
|
||
| @RepositoryAdapter | ||
| @RequiredArgsConstructor | ||
| public class EmailVerificationFinder { | ||
|
|
||
| private final EmailVerificationRepository mailRepository; | ||
|
|
||
| public EmailVerification findById(String email) { | ||
| return mailRepository.findById(email).orElseThrow(() -> new EmailVerificationException(INVALID_REQUEST)); | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/tiki/server/emailverification/adapter/EmailVerificationSaver.java
This file contains hidden or 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,17 @@ | ||
| package com.tiki.server.emailverification.adapter; | ||
|
|
||
| import com.tiki.server.common.support.RepositoryAdapter; | ||
| import com.tiki.server.emailverification.domain.EmailVerification; | ||
| import com.tiki.server.emailverification.repository.EmailVerificationRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @RepositoryAdapter | ||
| @RequiredArgsConstructor | ||
| public class EmailVerificationSaver { | ||
|
|
||
| private final EmailVerificationRepository mailRepository; | ||
|
|
||
| public void save(EmailVerification mail) { | ||
| mailRepository.save(mail); | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| package com.tiki.server.mail.constants; | ||
| package com.tiki.server.emailverification.constants; | ||
|
|
||
| public class MailConstants { | ||
| public class EmailConstants { | ||
|
|
||
| public static final String TIKI_EMAIL = "[email protected]"; | ||
| public static final String MAIL_SUBJECT_SIGN_UP = "[Ti.Ki] 회원가입: 이메일 인증번호 안내"; | ||
|
|
@@ -9,4 +9,9 @@ public class MailConstants { | |
| public static final String MAIL_FORMAT_EDU = ".edu"; | ||
| public static final String MAIL_FORMAT_AC_KR = ".ac.kr"; | ||
| public static final String TEMPLATE_NAME = "certification"; | ||
| public static final int INIT_NUM = 0; | ||
| public static final int CODE_LENGTH = 6; | ||
| public static final int CODE_NUM_MAX_VALUE_PER_WORD = 10; | ||
| public static final String CERTIFICATION_PAGE_LOGO_IMAGE_VAR = "image"; | ||
| public static final String CERTIFICATION_PAGE_CODEE_VAR = "code"; | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
37 changes: 37 additions & 0 deletions
37
src/main/java/com/tiki/server/emailverification/domain/EmailVerification.java
This file contains hidden or 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,37 @@ | ||
| package com.tiki.server.emailverification.domain; | ||
|
|
||
| import com.tiki.server.common.entity.Email; | ||
| import com.tiki.server.emailverification.exception.EmailVerificationException; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.Id; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import org.springframework.data.redis.core.RedisHash; | ||
|
|
||
| import static com.tiki.server.emailverification.message.ErrorCode.INVALID_MATCHED; | ||
| import static jakarta.persistence.GenerationType.IDENTITY; | ||
| import static lombok.AccessLevel.PRIVATE; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor(access = PRIVATE) | ||
| @Builder | ||
| @RedisHash(value = "mailVerification", timeToLive = 180) | ||
| public class EmailVerification { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = IDENTITY) | ||
| private String id; | ||
|
|
||
| private String code; | ||
|
|
||
| public static EmailVerification of(Email email, String code) { | ||
| return EmailVerification.builder().id(email.getEmail()).code(code).build(); | ||
| } | ||
|
|
||
| public void verify(String code){ | ||
| if(!this.code.equals(code)){ | ||
| throw new EmailVerificationException(INVALID_MATCHED); | ||
| } | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/tiki/server/emailverification/domain/MailSender.java
This file contains hidden or 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.tiki.server.emailverification.domain; | ||
|
|
||
| import com.tiki.server.common.entity.Email; | ||
| import com.tiki.server.emailverification.exception.EmailVerificationException; | ||
| import jakarta.mail.internet.MimeMessage; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.core.io.ClassPathResource; | ||
| import org.springframework.mail.javamail.JavaMailSender; | ||
| import org.springframework.mail.javamail.MimeMessageHelper; | ||
| import org.springframework.stereotype.Component; | ||
| import org.thymeleaf.context.Context; | ||
| import org.thymeleaf.spring6.SpringTemplateEngine; | ||
|
|
||
| import java.util.Random; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| import static com.tiki.server.emailverification.constants.EmailConstants.*; | ||
| import static com.tiki.server.emailverification.message.ErrorCode.MESSAGE_HELPER_ERROR; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class MailSender { | ||
|
|
||
| private final SpringTemplateEngine templateEngine; | ||
| private final JavaMailSender javaMailSender; | ||
|
|
||
| public EmailVerification sendVerificationMail(Email email, String subject) { | ||
| String code = generateRandomValue(); | ||
| MimeMessage message = makeMessage(email, code, subject); | ||
| javaMailSender.send(message); | ||
| return EmailVerification.of(email, code); | ||
| } | ||
|
|
||
| private static String generateRandomValue() { | ||
| Random random = new Random(); | ||
| return IntStream.range(INIT_NUM, CODE_LENGTH).mapToObj(i -> String.valueOf(random.nextInt(CODE_NUM_MAX_VALUE_PER_WORD))).collect(Collectors.joining()); | ||
| } | ||
|
|
||
| private MimeMessage makeMessage(Email email, String code, String subject) { | ||
| MimeMessage message = javaMailSender.createMimeMessage(); | ||
| try { | ||
| MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8"); | ||
| helper.setFrom(TIKI_EMAIL); | ||
| helper.setTo(email.getEmail()); | ||
| helper.setSubject(subject); | ||
| helper.setText(setContext(code), true); | ||
| helper.addInline(CERTIFICATION_PAGE_LOGO_IMAGE_VAR, new ClassPathResource(IMG_PATH)); | ||
| return message; | ||
| } catch (Exception e) { | ||
| throw new EmailVerificationException(MESSAGE_HELPER_ERROR); | ||
| } | ||
| } | ||
|
|
||
| private String setContext(String code) { | ||
| Context context = new Context(); | ||
| context.setVariable(CERTIFICATION_PAGE_CODEE_VAR, code); | ||
| return templateEngine.process(TEMPLATE_NAME, context); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.