-
Notifications
You must be signed in to change notification settings - Fork 2
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 #17 from PawWithU/feat/16-email-confirm-api
[Feature] 이메일 인증번호 발송 API 구현
- Loading branch information
Showing
11 changed files
with
186 additions
and
5 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
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
8 changes: 8 additions & 0 deletions
8
src/main/java/com/pawwithu/connectdog/domain/auth/dto/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.pawwithu.connectdog.domain.auth.dto.request; | ||
|
||
import jakarta.validation.constraints.Email; | ||
import jakarta.validation.constraints.NotBlank; | ||
|
||
public record EmailRequest(@Email(message="이메일 형식에 맞지 않습니다.") | ||
@NotBlank(message = "이메일은 필수 입력 값입니다.")String email) { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/pawwithu/connectdog/domain/auth/dto/response/EmailResponse.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,4 @@ | ||
package com.pawwithu.connectdog.domain.auth.dto.response; | ||
|
||
public record EmailResponse(String authCode) { | ||
} |
106 changes: 106 additions & 0 deletions
106
src/main/java/com/pawwithu/connectdog/domain/auth/service/EmailService.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,106 @@ | ||
package com.pawwithu.connectdog.domain.auth.service; | ||
|
||
import com.pawwithu.connectdog.domain.auth.dto.request.EmailRequest; | ||
import com.pawwithu.connectdog.domain.auth.dto.response.EmailResponse; | ||
import com.pawwithu.connectdog.domain.intermediary.repository.IntermediaryRepository; | ||
import com.pawwithu.connectdog.domain.volunteer.repository.VolunteerRepository; | ||
import com.pawwithu.connectdog.error.exception.custom.BadRequestException; | ||
import jakarta.mail.MessagingException; | ||
import jakarta.mail.internet.MimeMessage; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.stereotype.Service; | ||
import org.thymeleaf.context.Context; | ||
import org.thymeleaf.spring6.SpringTemplateEngine; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.util.Random; | ||
|
||
import static com.pawwithu.connectdog.error.ErrorCode.ALREADY_EXIST_EMAIL; | ||
import static com.pawwithu.connectdog.error.ErrorCode.EMAIL_SEND_ERROR; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class EmailService { | ||
|
||
private final JavaMailSender emailSender; | ||
private final SpringTemplateEngine templateEngine; | ||
private String authNum; //랜덤 인증 코드 | ||
private final VolunteerRepository volunteerRepository; | ||
private final IntermediaryRepository intermediaryRepository; | ||
|
||
/** | ||
* 랜덤 인증 코드 생성 | ||
*/ | ||
private void createCode() { | ||
Random random = new Random(); | ||
StringBuffer key = new StringBuffer(); | ||
|
||
for(int i = 0; i < 8; i++) { // 8자리 | ||
int index = random.nextInt(3); | ||
|
||
switch (index) { | ||
case 0 : // 소문자 | ||
key.append((char) ((int)random.nextInt(26) + 97)); | ||
break; | ||
case 1: // 대문자 | ||
key.append((char) ((int)random.nextInt(26) + 65)); | ||
break; | ||
case 2: // 0-9 숫자 | ||
key.append(random.nextInt(9)); | ||
break; | ||
} | ||
} | ||
authNum = key.toString(); | ||
} | ||
|
||
/** | ||
* 메일 양식 작성 | ||
*/ | ||
private MimeMessage createEmailForm(String email) throws MessagingException, UnsupportedEncodingException { | ||
|
||
createCode(); //인증 코드 생성 | ||
String setFrom = "[email protected]"; // email-config에 설정한 자신의 이메일 주소 | ||
String toEmail = email; // 받는 사람 | ||
String title = "ConnectDog\uD83D\uDC3E 회원가입 인증 번호입니다."; // 제목 | ||
|
||
MimeMessage message = emailSender.createMimeMessage(); | ||
message.addRecipients(MimeMessage.RecipientType.TO, toEmail); // 보낼 이메일 설정 | ||
message.setSubject(title); | ||
message.setFrom(setFrom); | ||
message.setText(setContext(authNum), "utf-8", "html"); | ||
|
||
return message; | ||
} | ||
|
||
/** | ||
* 메일 전송 | ||
*/ | ||
public EmailResponse sendEmail(EmailRequest emailRequest) throws BadRequestException { | ||
// 이메일 중복 검사 | ||
if (volunteerRepository.existsByEmail(emailRequest.email())) { | ||
throw new BadRequestException(ALREADY_EXIST_EMAIL); | ||
} | ||
if (intermediaryRepository.existsByEmail(emailRequest.email())) { | ||
throw new BadRequestException(ALREADY_EXIST_EMAIL); | ||
} | ||
try{ | ||
// 메일전송에 필요한 정보 설정 | ||
MimeMessage emailForm = createEmailForm(emailRequest.email()); | ||
emailSender.send(emailForm); | ||
return new EmailResponse(authNum); | ||
}catch (UnsupportedEncodingException | MessagingException e){ | ||
throw new BadRequestException(EMAIL_SEND_ERROR); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* context 설정 | ||
*/ | ||
private String setContext(String code) { | ||
Context context = new Context(); | ||
context.setVariable("code", code); | ||
return templateEngine.process("mail", 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
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,19 @@ | ||
<!DOCTYPE html> | ||
<html xmlns:th="http://www.thymeleaf.org"> | ||
|
||
<body> | ||
<div style="margin:100px;"> | ||
<h1> 안녕하세요. ConnectDog🐾입니다.</h1> | ||
<br> | ||
<p> 아래 코드를 회원가입 창으로 돌아가 입력해주세요.</p> | ||
<br> | ||
|
||
<div align="center" style="border:1px solid black; font-family:verdana;"> | ||
<h3 style="color:blue"> 회원가입 인증 코드 입니다. </h3> | ||
<div style="font-size:130%" th:text="${code}"> </div> | ||
</div> | ||
<br/> | ||
</div> | ||
|
||
</body> | ||
</html> |