-
Notifications
You must be signed in to change notification settings - Fork 6
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
feat-be: 이메일 인증 기능 구현 #848
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
435bb2e
Create draft PR for #846
github-actions[bot] 7fa1c30
feat: 이메일 인증 번호 전송 구현
Chocochip101 5898a69
feat: 이메일 인증 번호 인증 구현
Chocochip101 1b5b8b5
refactor: 인증 코드 생성 로직 분리
Chocochip101 ab33ac7
test: 예외 케이스 테스트 생성
Chocochip101 071ca4f
chore: local redis 설정 추가
Chocochip101 bf9826f
test: MockBean으로 변경
Chocochip101 c0cc14e
chore: redis 개발 환경 추가
Chocochip101 d8ce77f
test: 인증 코드 성공 테스트 추가
Chocochip101 0ec1da2
refactor: verify 로직 위치 변경
Chocochip101 d42ee5c
feat: 템플릿 스타일 적용
Chocochip101 ae42fa9
refactor: 불필요 주석 제거
Chocochip101 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 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 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 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 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
...ru/email/controller/dto/EmailRequest.java → ...mail/controller/request/EmailRequest.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
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/com/cruru/email/controller/request/SendVerificationCodeRequest.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,12 @@ | ||
package com.cruru.email.controller.request; | ||
|
||
import jakarta.validation.constraints.Email; | ||
import jakarta.validation.constraints.NotBlank; | ||
|
||
public record SendVerificationCodeRequest( | ||
@NotBlank | ||
String email | ||
) { | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
backend/src/main/java/com/cruru/email/controller/request/VerifyCodeRequest.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,15 @@ | ||
package com.cruru.email.controller.request; | ||
|
||
import jakarta.validation.constraints.Email; | ||
import jakarta.validation.constraints.NotBlank; | ||
|
||
public record VerifyCodeRequest( | ||
@NotBlank | ||
String email, | ||
|
||
@NotBlank | ||
String verificationCode | ||
) { | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...src/main/java/com/cruru/email/exception/badrequest/VerificationCodeMismatchException.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,12 @@ | ||
package com.cruru.email.exception.badrequest; | ||
|
||
import com.cruru.advice.badrequest.BadRequestException; | ||
|
||
public class VerificationCodeMismatchException extends BadRequestException { | ||
|
||
private static final String MESSAGE = "인증 코드가 일치하지 않습니다."; | ||
|
||
public VerificationCodeMismatchException() { | ||
super(MESSAGE); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...src/main/java/com/cruru/email/exception/badrequest/VerificationCodeNotFoundException.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,12 @@ | ||
package com.cruru.email.exception.badrequest; | ||
|
||
import com.cruru.advice.badrequest.BadRequestException; | ||
|
||
public class VerificationCodeNotFoundException extends BadRequestException { | ||
|
||
private static final String MESSAGE = "인증 코드가 존재하지 않거나 만료되었습니다."; | ||
|
||
public VerificationCodeNotFoundException() { | ||
super(MESSAGE); | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
backend/src/main/java/com/cruru/email/service/EmailRedisClient.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,25 @@ | ||
package com.cruru.email.service; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class EmailRedisClient { | ||
|
||
private static final String REDIS_PREFIX = "email_verification:"; | ||
private static final long VERIFICATION_CODE_EXPIRATION = 10; | ||
|
||
private final RedisTemplate<String, String> redisTemplate; | ||
|
||
public void saveVerificationCode(String email, String verificationCode) { | ||
redisTemplate.opsForValue() | ||
.set(REDIS_PREFIX + email, verificationCode, VERIFICATION_CODE_EXPIRATION, TimeUnit.MINUTES); | ||
} | ||
|
||
public String getVerificationCode(String email) { | ||
return redisTemplate.opsForValue().get(REDIS_PREFIX + email); | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
backend/src/main/java/com/cruru/email/util/EmailTemplate.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,28 @@ | ||
package com.cruru.email.util; | ||
|
||
public class EmailTemplate { | ||
|
||
public static String generateVerificationEmailContent(String verificationCode) { | ||
return """ | ||
<div style='font-family: Arial, sans-serif; padding: 20px; background-color: #ffffff; border: 1px solid #e0e0e0; max-width: 600px; margin: 0 auto;'> | ||
<div style='text-align: center; padding: 20px 0; background-color: #AA2298; color: white;'> | ||
<h1 style='margin: 0;'>크루루</h1> | ||
</div> | ||
<div style='padding: 30px; text-align: center;'> | ||
<h2 style='color: #333;'>[크루루] 인증 코드 안내</h2> | ||
<p style='font-size: 16px; color: #555;'>안녕하세요,</p> | ||
<p style='font-size: 16px; color: #555;'>아래 인증 코드를 입력해 주세요:</p> | ||
<div style='padding: 20px; background-color: #f0f0f0; border-radius: 10px; display: inline-block; margin: 20px 0;'> | ||
<span id='verificationCode' style='font-size: 32px; font-weight: bold; color: #333;'>%s</span> | ||
</div> | ||
<p style='font-size: 14px; color: #888;'>이 코드는 10분 후에 만료됩니다.</p> | ||
</div> | ||
<hr style='border: none; border-top: 1px solid #ddd; margin: 20px 0;'> | ||
<div style='padding: 20px; text-align: center; font-size: 12px; color: #888;'> | ||
<p>본 메일은 크루루 시스템에 의해 자동 발송되었습니다.</p> | ||
<p>© 2024 크루루. All Rights Reserved.</p> | ||
</div> | ||
</div> | ||
""".formatted(verificationCode); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
backend/src/main/java/com/cruru/email/util/VerificationCodeUtil.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,28 @@ | ||
package com.cruru.email.util; | ||
|
||
import com.cruru.email.exception.badrequest.VerificationCodeMismatchException; | ||
import com.cruru.email.exception.badrequest.VerificationCodeNotFoundException; | ||
import java.util.Random; | ||
|
||
public class VerificationCodeUtil { | ||
|
||
private static final Random random = new Random(); | ||
private static final int CODE_LENGTH = 6; | ||
|
||
private VerificationCodeUtil() { | ||
} | ||
|
||
public static String generateVerificationCode() { | ||
return String.format("%0" + CODE_LENGTH + "d", random.nextInt((int) Math.pow(10, CODE_LENGTH))); | ||
Chocochip101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public static void verify(String storedVerificationCode, String inputVerificationCode) { | ||
if (storedVerificationCode == null) { | ||
throw new VerificationCodeNotFoundException(); | ||
} | ||
|
||
if (!storedVerificationCode.equals(inputVerificationCode)) { | ||
throw new VerificationCodeMismatchException(); | ||
} | ||
} | ||
} |
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
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.
10분인 이유가 있나요? 단순 궁금증입니다.
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.
10분이 인증하기에 적당한 시간이여서 설정했습니다. 더 좋은 시간이 있다면 반영하겠습니다~