-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Domitory-CheckMate/feature/6-user
[feat] 이메일 인증 기능 구현
- Loading branch information
Showing
10 changed files
with
150 additions
and
3 deletions.
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
26 changes: 26 additions & 0 deletions
26
src/main/java/org/gachon/checkmate/domain/member/controller/MemberController.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,26 @@ | ||
package org.gachon.checkmate.domain.member.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.gachon.checkmate.domain.member.dto.request.EmailPostRequestDto; | ||
import org.gachon.checkmate.domain.member.dto.response.EmailResponseDto; | ||
import org.gachon.checkmate.domain.member.service.MemberService; | ||
import org.gachon.checkmate.global.common.SuccessResponse; | ||
import org.springframework.http.ResponseEntity; | ||
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; | ||
|
||
@RequiredArgsConstructor | ||
@RequestMapping("/api/member") | ||
@RestController | ||
public class MemberController { | ||
|
||
private final MemberService memberService; | ||
|
||
@PostMapping("/email") | ||
public ResponseEntity<SuccessResponse<?>> sendMail(@RequestBody final EmailPostRequestDto emailPostRequestDto) { | ||
final EmailResponseDto emailResponseDto = memberService.sendMail(emailPostRequestDto); | ||
return SuccessResponse.ok(emailResponseDto); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/org/gachon/checkmate/domain/member/dto/request/EmailPostRequestDto.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,6 @@ | ||
package org.gachon.checkmate.domain.member.dto.request; | ||
|
||
public record EmailPostRequestDto( | ||
String email | ||
) { | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/org/gachon/checkmate/domain/member/dto/response/EmailResponseDto.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,6 @@ | ||
package org.gachon.checkmate.domain.member.dto.response; | ||
|
||
public record EmailResponseDto( | ||
String code | ||
) { | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/org/gachon/checkmate/domain/member/service/MemberService.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,26 @@ | ||
package org.gachon.checkmate.domain.member.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.gachon.checkmate.domain.member.dto.request.EmailPostRequestDto; | ||
import org.gachon.checkmate.domain.member.dto.response.EmailResponseDto; | ||
import org.gachon.checkmate.global.config.auth.jwt.JwtProvider; | ||
import org.gachon.checkmate.global.config.mail.MailProvider; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Transactional | ||
@Service | ||
public class MemberService { | ||
|
||
private final JwtProvider jwtProvider; | ||
private final MailProvider mailProvider; | ||
|
||
public EmailResponseDto sendMail(EmailPostRequestDto emailPostRequestDto) { | ||
String authNum = mailProvider.sendMail(emailPostRequestDto.email(), "email"); | ||
return new EmailResponseDto(authNum); | ||
} | ||
|
||
} |
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
57 changes: 57 additions & 0 deletions
57
src/main/java/org/gachon/checkmate/global/config/mail/MailProvider.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,57 @@ | ||
package org.gachon.checkmate.global.config.mail; | ||
|
||
import jakarta.mail.MessagingException; | ||
import jakarta.mail.internet.MimeMessage; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.gachon.checkmate.domain.member.dto.request.EmailPostRequestDto; | ||
import org.gachon.checkmate.global.error.exception.InternalServerException; | ||
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 static org.gachon.checkmate.global.error.ErrorCode.EMAIL_SEND_ERROR; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Component | ||
public class MailProvider { | ||
|
||
private final JavaMailSender javaMailSender; | ||
private final SpringTemplateEngine templateEngine; | ||
|
||
public String sendMail(String email, String type) { | ||
String authNum = createNumericCode(); | ||
MimeMessage mimeMessage = javaMailSender.createMimeMessage(); | ||
try { | ||
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, false, "UTF-8"); | ||
mimeMessageHelper.setTo(email); // 메일 수신자 | ||
mimeMessageHelper.setSubject("[CHECKMATE] 이메일 인증번호 발송"); // 메일 제목 | ||
mimeMessageHelper.setText(setContext(authNum, type), true); // 메일 본문 | ||
javaMailSender.send(mimeMessage); | ||
return authNum; | ||
} catch (MessagingException e) { | ||
throw new InternalServerException(EMAIL_SEND_ERROR); | ||
} | ||
} | ||
|
||
private static String createNumericCode() { | ||
Random random = new Random(); | ||
StringBuilder code = new StringBuilder(); | ||
for (int i = 0; i < 6; i++) { | ||
code.append(random.nextInt(10)); | ||
} | ||
return code.toString(); | ||
} | ||
|
||
private String setContext(String code, String type) { | ||
Context context = new Context(); | ||
context.setVariable("code", code); | ||
return templateEngine.process(type, context); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!DOCTYPE html> | ||
<html xmlns:th="http://www.thymeleaf.org"> | ||
|
||
<body style="font-family: Arial, sans-serif; background-color: #f8f8f8; text-align: center;"> | ||
|
||
<div style="max-width: 600px; margin: 100px auto; padding: 20px; background-color: #ffffff; border: 1px solid #ff6600; border-radius: 10px;"> | ||
<p style="color: #ff6600;"><strong>기숙사 룸메이트 매칭 플랫폼 Check-mate</strong></p> | ||
<p style="color: #000000;">반갑습니다.</p> | ||
<p>아래 코드를 인증번호 입력 란에 입력해주세요.</p> | ||
|
||
<div style="font-size: 24px; margin-top: 20px; padding: 10px; background-color: #ff6600; color: #ffffff; border-radius: 5px;"> | ||
<span th:text="${code}"></span> | ||
</div> | ||
</div> | ||
|
||
</body> | ||
|
||
</html> |