Repository navigation
[Feat/#209] 회원가입 이메일 등록과 이메일 인증 #210
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
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
26efd70
feat: 429 에러 상태 코드 추가
tnals0924 3a66aa7
feat: 메일 발송 클라이언트 추가
tnals0924 ec94ca5
feat: 이메일 인증 기록 저장 추가
tnals0924 941af3f
feat: 이메일 인증 코드 발송·확인 API 추가
tnals0924 c704d3d
feat: 회원가입에 인증한 이메일 등록 추가
tnals0924 52449ef
feat: 메일 발송 일시 오류 재시도 추가
tnals0924 509b327
feat: 인증 메일 비동기 발송
tnals0924 6ba986f
feat: 인증 메일 발송 대기열이 가득 차면 재시도
tnals0924 0f12918
feat: 재시도 실행기 RetryExecutor 추가
tnals0924 b09d55b
refactor: 메일 발송 재시도를 RetryExecutor로 변경
tnals0924 a6454c5
feat: SMTP 접속 정보 환경 변수 추가
tnals0924 d6a9b37
chore: Flyway 마이그레이션 버전 조정
tnals0924 68c53f1
Merge branch 'main' into feat/#209-email-verification
tnals0924 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
2 changes: 1 addition & 1 deletion
2
...api/src/main/java/kr/ac/kookmin/stream/api/app/core/auth/response/OAuthLoginResponse.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
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
15 changes: 14 additions & 1 deletion
15
...i/src/main/java/kr/ac/kookmin/stream/api/app/core/member/usecase/MemberSignUpUseCase.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 |
|---|---|---|
| @@ -1,26 +1,39 @@ | ||
| package kr.ac.kookmin.stream.api.app.core.member.usecase; | ||
|
|
||
| import kr.ac.kookmin.stream.common.BusinessException; | ||
| import kr.ac.kookmin.stream.member.domain.member.domain.MemberSignUpCommand; | ||
| import kr.ac.kookmin.stream.member.domain.member.service.MemberService; | ||
| import kr.ac.kookmin.stream.member.domain.notification.service.MemberNotificationSettingService; | ||
| import kr.ac.kookmin.stream.member.domain.verification.domain.EmailVerificationErrorCode; | ||
| import kr.ac.kookmin.stream.member.domain.verification.service.EmailVerificationService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| /** | ||
| * 회원가입. {@code member}(전화번호, 약관 동의)와 {@code notification}(기본 알림 설정) 두 도메인을 조합한다. | ||
| * 회원가입. {@code member}(전화번호·이메일, 약관 동의), {@code verification}(이메일 인증 여부), | ||
| * {@code notification}(기본 알림 설정) 세 도메인을 조합한다. | ||
| */ | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class MemberSignUpUseCase { | ||
|
|
||
| private final MemberService memberService; | ||
| private final EmailVerificationService emailVerificationService; | ||
| private final MemberNotificationSettingService memberNotificationSettingService; | ||
|
|
||
| // 가입을 마친 회원에게는 항상 알림 설정이 있도록 한 트랜잭션으로 묶는다 | ||
| @Transactional | ||
| public void signUp(Long memberId, MemberSignUpCommand command) { | ||
| validateEmailVerified(memberId, command.email()); | ||
| memberService.signUp(memberId, command); | ||
| memberNotificationSettingService.createDefaultIfAbsent(memberId); | ||
| } | ||
|
|
||
| private void validateEmailVerified(Long memberId, String email) { | ||
| boolean registeredEmail = email.equalsIgnoreCase(memberService.getById(memberId).getEmail()); | ||
| if (!registeredEmail && !emailVerificationService.isVerifiedForSignUp(memberId, email)) { | ||
| throw new BusinessException(EmailVerificationErrorCode.EMAIL_NOT_VERIFIED); | ||
| } | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
...src/main/java/kr/ac/kookmin/stream/api/app/core/verification/AppEmailVerificationApi.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,51 @@ | ||
| package kr.ac.kookmin.stream.api.app.core.verification; | ||
|
|
||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import kr.ac.kookmin.stream.api.app.AppApiUser; | ||
| import kr.ac.kookmin.stream.api.app.core.verification.request.EmailVerificationConfirmRequest; | ||
| import kr.ac.kookmin.stream.api.app.core.verification.request.EmailVerificationSendRequest; | ||
| import kr.ac.kookmin.stream.api.common.dto.ApiResponse; | ||
| import kr.ac.kookmin.stream.api.common.openapi.ApiErrorCode; | ||
| import kr.ac.kookmin.stream.common.CommonErrorCode; | ||
| import kr.ac.kookmin.stream.member.domain.member.domain.MemberErrorCode; | ||
| import kr.ac.kookmin.stream.member.domain.verification.domain.EmailVerificationErrorCode; | ||
|
|
||
| /** | ||
| * 학생 앱 이메일 인증 API의 문서 명세. 구현은 {@link AppEmailVerificationController}가 맡는다. | ||
| */ | ||
| @Tag(name = "이메일 인증", description = "회원가입 전 이메일 인증 코드 발송·확인") | ||
| public interface AppEmailVerificationApi { | ||
|
|
||
| /** 인증 코드 발송. */ | ||
| @Operation(summary = "이메일 인증 코드 발송", | ||
| description = """ | ||
| 로그인한 회원이 입력한 이메일로 6자리 인증 코드를 보낸다. 코드는 5분 동안 유효하다. | ||
| 다시 보내려면 1분을 기다려야 하고, 하루(자정 기준)에 10번까지 보낼 수 있다. | ||
| 새 코드를 받으면 앞서 받은 코드는 쓸 수 없다. 다른 회원이 쓰는 이메일로는 보낼 수 없다. | ||
| 메일은 응답한 뒤에 보내므로 성공 응답이 메일 도착을 보장하지 않는다. 메일이 오지 않으면 1분 뒤 다시 요청한다. | ||
| EMAIL_SEND_FAILED는 발송 요청이 몰려 받지 못했을 때 응답한다.""") | ||
| @ApiErrorCode(type = CommonErrorCode.class, codes = {"INVALID_INPUT"}) | ||
| @ApiErrorCode(type = MemberErrorCode.class, codes = {"MEMBER_NOT_FOUND", "EMAIL_ALREADY_EXISTS"}) | ||
| @ApiErrorCode(type = EmailVerificationErrorCode.class, codes = { | ||
| "EMAIL_VERIFICATION_COOLDOWN", | ||
| "EMAIL_VERIFICATION_DAILY_LIMIT_EXCEEDED", | ||
| "EMAIL_SEND_FAILED" | ||
| }) | ||
| ApiResponse<Void> sendCode(AppApiUser apiUser, EmailVerificationSendRequest request); | ||
|
|
||
| /** 인증 코드 확인. */ | ||
| @Operation(summary = "이메일 인증 코드 확인", | ||
| description = """ | ||
| 가장 최근에 받은 인증 코드를 확인한다. 이메일은 코드를 요청할 때와 같아야 한다. | ||
| 코드를 5번 틀리면 코드를 다시 요청해야 한다. 이미 인증한 요청을 다시 보내면 성공으로 응답한다. | ||
| 인증한 뒤 30분 안에 회원가입을 마쳐야 한다.""") | ||
| @ApiErrorCode(type = CommonErrorCode.class, codes = {"INVALID_INPUT"}) | ||
| @ApiErrorCode(type = EmailVerificationErrorCode.class, codes = { | ||
| "EMAIL_VERIFICATION_NOT_FOUND", | ||
| "EMAIL_VERIFICATION_EXPIRED", | ||
| "EMAIL_VERIFICATION_CODE_MISMATCH", | ||
| "EMAIL_VERIFICATION_ATTEMPTS_EXCEEDED" | ||
| }) | ||
| ApiResponse<Void> confirm(AppApiUser apiUser, EmailVerificationConfirmRequest request); | ||
| } |
43 changes: 43 additions & 0 deletions
43
...n/java/kr/ac/kookmin/stream/api/app/core/verification/AppEmailVerificationController.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,43 @@ | ||
| package kr.ac.kookmin.stream.api.app.core.verification; | ||
|
|
||
| import jakarta.validation.Valid; | ||
| import kr.ac.kookmin.stream.api.app.AppApiUser; | ||
| import kr.ac.kookmin.stream.api.app.core.verification.request.EmailVerificationConfirmRequest; | ||
| import kr.ac.kookmin.stream.api.app.core.verification.request.EmailVerificationSendRequest; | ||
| import kr.ac.kookmin.stream.api.app.core.verification.usecase.EmailVerificationSendUseCase; | ||
| import kr.ac.kookmin.stream.api.common.dto.ApiResponse; | ||
| import kr.ac.kookmin.stream.member.domain.verification.service.EmailVerificationService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/v1/app/members/email-verifications") | ||
| @RequiredArgsConstructor | ||
| public class AppEmailVerificationController implements AppEmailVerificationApi { | ||
|
|
||
| private final EmailVerificationService emailVerificationService; | ||
| private final EmailVerificationSendUseCase emailVerificationSendUseCase; | ||
|
|
||
| @Override | ||
| @PostMapping | ||
| public ApiResponse<Void> sendCode( | ||
| AppApiUser apiUser, | ||
| @Valid @RequestBody EmailVerificationSendRequest request | ||
| ) { | ||
| emailVerificationSendUseCase.sendCode(apiUser.userId(), request.toCommand()); | ||
| return ApiResponse.success(); | ||
| } | ||
|
|
||
| @Override | ||
| @PostMapping("/confirm") | ||
| public ApiResponse<Void> confirm( | ||
| AppApiUser apiUser, | ||
| @Valid @RequestBody EmailVerificationConfirmRequest request | ||
| ) { | ||
| emailVerificationService.confirm(apiUser.userId(), request.toCommand()); | ||
| return ApiResponse.success(); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
.../ac/kookmin/stream/api/app/core/verification/request/EmailVerificationConfirmRequest.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,23 @@ | ||
| package kr.ac.kookmin.stream.api.app.core.verification.request; | ||
|
|
||
| import jakarta.validation.constraints.Email; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.Pattern; | ||
| import jakarta.validation.constraints.Size; | ||
| import kr.ac.kookmin.stream.member.domain.verification.domain.EmailVerificationConfirmCommand; | ||
|
|
||
| public record EmailVerificationConfirmRequest( | ||
| @NotBlank(message = "이메일을 입력해 주세요.") | ||
| @Email(message = "이메일 형식이 올바르지 않습니다.") | ||
| @Size(max = 255, message = "이메일은 255자 이하로 입력해 주세요.") | ||
| String email, | ||
|
|
||
| @NotBlank(message = "인증 코드를 입력해 주세요.") | ||
| @Pattern(regexp = "^\\d{6}$", message = "인증 코드는 숫자 6자리입니다.") | ||
| String code | ||
| ) { | ||
|
|
||
| public EmailVerificationConfirmCommand toCommand() { | ||
| return new EmailVerificationConfirmCommand(email, code); | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
.../kr/ac/kookmin/stream/api/app/core/verification/request/EmailVerificationSendRequest.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,18 @@ | ||
| package kr.ac.kookmin.stream.api.app.core.verification.request; | ||
|
|
||
| import jakarta.validation.constraints.Email; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.Size; | ||
| import kr.ac.kookmin.stream.member.domain.verification.domain.EmailVerificationSendCommand; | ||
|
|
||
| public record EmailVerificationSendRequest( | ||
| @NotBlank(message = "이메일을 입력해 주세요.") | ||
| @Email(message = "이메일 형식이 올바르지 않습니다.") | ||
| @Size(max = 255, message = "이메일은 255자 이하로 입력해 주세요.") | ||
| String email | ||
| ) { | ||
|
|
||
| public EmailVerificationSendCommand toCommand() { | ||
| return new EmailVerificationSendCommand(email); | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
.../kr/ac/kookmin/stream/api/app/core/verification/usecase/EmailVerificationSendUseCase.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,31 @@ | ||
| package kr.ac.kookmin.stream.api.app.core.verification.usecase; | ||
|
|
||
| import kr.ac.kookmin.stream.common.BusinessException; | ||
| import kr.ac.kookmin.stream.member.domain.member.domain.MemberErrorCode; | ||
| import kr.ac.kookmin.stream.member.domain.member.service.MemberService; | ||
| import kr.ac.kookmin.stream.member.domain.verification.domain.EmailVerificationSendCommand; | ||
| import kr.ac.kookmin.stream.member.domain.verification.service.EmailVerificationService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| /** | ||
| * 이메일 인증 코드 발송. {@code member}(이메일 중복 확인)와 {@code verification}(코드 발송) 두 도메인을 조합한다. | ||
| */ | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class EmailVerificationSendUseCase { | ||
|
|
||
| private final MemberService memberService; | ||
| private final EmailVerificationService emailVerificationService; | ||
|
|
||
| public void sendCode(Long memberId, EmailVerificationSendCommand command) { | ||
| validateEmailNotUsedByOthers(memberId, command.email()); | ||
| emailVerificationService.sendCode(memberId, command); | ||
| } | ||
|
|
||
| private void validateEmailNotUsedByOthers(Long memberId, String email) { | ||
| if (memberService.isEmailUsedByOthers(memberId, email)) { | ||
| throw new BusinessException(MemberErrorCode.EMAIL_ALREADY_EXISTS); | ||
| } | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
bootstrap/src/main/java/kr/ac/kookmin/stream/bootstrap/config/AsyncConfig.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,48 @@ | ||
| package kr.ac.kookmin.stream.bootstrap.config; | ||
|
|
||
| import java.util.Map; | ||
| import kr.ac.kookmin.stream.common.TaskExecutorNames; | ||
| import org.slf4j.MDC; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.scheduling.annotation.EnableAsync; | ||
| import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
|
||
| /** | ||
| * {@code @Async}를 켜고 작업별 실행기를 등록한다. 빈 이름은 {@link TaskExecutorNames}에 둔다. | ||
| */ | ||
| @Configuration | ||
| @EnableAsync | ||
| public class AsyncConfig { | ||
|
|
||
| private static final int EMAIL_POOL_SIZE = 2; | ||
| private static final int EMAIL_QUEUE_CAPACITY = 100; | ||
| private static final int EMAIL_AWAIT_TERMINATION_SECONDS = 10; | ||
|
|
||
| @Bean(TaskExecutorNames.EMAIL_TASK_EXECUTOR) | ||
| public ThreadPoolTaskExecutor emailTaskExecutor() { | ||
| ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
| executor.setThreadNamePrefix("email-"); | ||
| executor.setCorePoolSize(EMAIL_POOL_SIZE); | ||
| executor.setMaxPoolSize(EMAIL_POOL_SIZE); | ||
| executor.setQueueCapacity(EMAIL_QUEUE_CAPACITY); | ||
| executor.setTaskDecorator(AsyncConfig::copyMdc); | ||
| executor.setWaitForTasksToCompleteOnShutdown(true); | ||
| executor.setAwaitTerminationSeconds(EMAIL_AWAIT_TERMINATION_SECONDS); | ||
| return executor; | ||
| } | ||
|
|
||
| private static Runnable copyMdc(Runnable task) { | ||
| Map<String, String> context = MDC.getCopyOfContextMap(); | ||
| return () -> { | ||
| if (context != null) { | ||
| MDC.setContextMap(context); | ||
| } | ||
| try { | ||
| task.run(); | ||
| } finally { | ||
| MDC.clear(); | ||
| } | ||
| }; | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: billilge/stream-server
Length of output: 4160
Script executed:
Repository: billilge/stream-server
Length of output: 21032
Script executed:
Repository: billilge/stream-server
Length of output: 29779
이관 회원의 기존 이메일 표기를 덮어쓰지 마세요.
인증 불일치 문제는 발생하지 않습니다. 세 command가 이메일을 모두 소문자로 정규화합니다.
다만 이관 회원은 기존 이메일과 대소문자만 다른 값을 보내도 가입할 수 있습니다. 이후
signUp이 기존 이메일을 정규화된 값으로 덮어쓰므로, 이관 이메일의 기존 표기가 변경됩니다. 대소문자가 같은 이메일이면 저장을 건너뛰고, 다른 이메일만 갱신하세요.Suggested fix
Source: Learnings