Skip to content
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

회원가입 인증코드 이메일 HTML 적용, 멤버 인증 테스트 작성 #131

Merged
merged 3 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public ResponseEntity<ResultResponse> sendResetPasswordCode(

@ApiOperation(value = "코드를 통한 비밀번호 재설정")
@ApiImplicitParam(name = "Authorization", value = "불필요", required = false, example = " ")
@PostMapping(value = "/accounts/password/reset")
@PutMapping(value = "/accounts/password/reset")
public ResponseEntity<ResultResponse> resetPassword(@Validated @RequestBody ResetPasswordRequest resetPasswordRequest, HttpServletResponse response) {
JwtDto jwt = memberAuthService.resetPassword(resetPasswordRequest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ public enum ErrorCode {
CANT_CONVERT_FILE(500, "FI001", "파일을 변환할 수 없습니다."),

// Alarm
MISMATCHED_ALARM_TYPE(400, "A001", "알람 형식이 올바르지 않습니다.")
MISMATCHED_ALARM_TYPE(400, "A001", "알람 형식이 올바르지 않습니다."),

// Email
CANT_SEND_EMAIL(500, "E001", "이메일 전송 중 오류가 발생했습니다.")
;

private int status;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cloneproject.Instagram.exception;

import cloneproject.Instagram.dto.error.ErrorCode;

public class CantSendEmailException extends BusinessException{
public CantSendEmailException(){
super(ErrorCode.CANT_SEND_EMAIL);
}

}
20 changes: 13 additions & 7 deletions src/main/java/cloneproject/Instagram/service/EmailCodeService.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package cloneproject.Instagram.service;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.Random;

import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;

import cloneproject.Instagram.entity.member.Member;
import cloneproject.Instagram.entity.redis.EmailCode;
import cloneproject.Instagram.entity.redis.ResetPasswordCode;
import cloneproject.Instagram.exception.CantResetPasswordException;
import cloneproject.Instagram.exception.CantSendEmailException;
import cloneproject.Instagram.exception.MemberDoesNotExistException;
import cloneproject.Instagram.exception.NoConfirmEmailException;
import cloneproject.Instagram.repository.EmailCodeRedisRepository;
Expand All @@ -29,21 +33,23 @@ public class EmailCodeService {
private final EmailService emailService;

public boolean sendEmailConfirmationCode(String username, String email){

String text;
String code = createConfirmationCode(6);

try{
ClassPathResource resource = new ClassPathResource("confirmEmailUI.html");
String html = new String(resource.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
text = String.format(html, email, code, email);
}catch(IOException e){
throw new CantSendEmailException();
}
EmailCode emailCode = EmailCode.builder()
.username(username)
.email(email)
.code(code)
.build();
emailCodeRedisRepository.save(emailCode);

SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setTo(email);
mailMessage.setSubject("회원가입 이메일 인증코드");
mailMessage.setText(code);
emailService.sendEmail(mailMessage);
emailService.sendHtmlTextEmail(username+ ", welcome to Instagram." ,text, email);

return true;
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/cloneproject/Instagram/service/EmailService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package cloneproject.Instagram.service;

import javax.mail.internet.MimeMessage;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import cloneproject.Instagram.exception.CantSendEmailException;
import lombok.RequiredArgsConstructor;

@Service
Expand All @@ -18,4 +22,18 @@ public void sendEmail(SimpleMailMessage email){
javaMailSender.send(email);
}

@Async
public void sendHtmlTextEmail(String subject, String content, String email){
MimeMessage message = javaMailSender.createMimeMessage();
try {
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "UTF-8");
messageHelper.setTo(email);
messageHelper.setSubject(subject);
messageHelper.setText(content, true);
javaMailSender.send(message);
}catch(Exception e){
throw new CantSendEmailException();
}
}

}
281 changes: 281 additions & 0 deletions src/main/resources/confirmEmailUI.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="Content-Type" content="text/html charset=UTF-8" />
<title>Instagram</title>
</head>
<body>
<table
border="0"
width="100%%"
cellspacing="0"
cellpadding="0"
style="border-collapse: collapse"
>
<tbody>
<tr>
<td height="20" colspan="3" style="line-height: 20px">&nbsp;</td>
</tr>
<tr>
<td height="1" colspan="3" style="line-height: 1px"></td>
</tr>
<tr>
<td>
<table
border="0"
width="100%%"
cellspacing="0"
cellpadding="0"
style="
border-collapse: collapse;
text-align: center;
width: 100%%;
max-width: 433px;
margin-top: 0;
margin-left: auto;
margin-bottom: 0;
margin-right: auto;
"
>
<tbody>
<tr>
<td width="15"></td>
<td style="line-height: 0px; padding: 0 0 15px 0">
<table
border="0"
width="100%%"
cellspacing="0"
cellpadding="0"
style="border-collapse: collapse"
>
<tbody>
<tr>
<td
style="width: 100%%; text-align: left; height: 33px"
>
<img
src="http://drive.google.com/uc?export=view&id=1oArh9BFHw1K2Gc5dsYzs_ZQMt6JAuQeE"
height="33"
style="border: 0"
/>
</td>
</tr>
</tbody>
</table>
</td>
<td width="15"></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table
border="0"
width="430"
cellspacing="0"
cellpadding="0"
style="
border-collapse: collapse;
margin-top: 0;
margin-left: auto;
margin-bottom: 0;
margin-right: auto;
"
>
<tbody>
<tr>
<td>
<table
border="0"
width="430"
cellspacing="0"
cellpadding="0"
style="
border-collapse: collapse;
margin-top: 0;
margin-left: auto;
margin-bottom: 0;
margin-right: auto;
width: 430;
"
>
<tbody>
<tr>
<td width="15" style="display: block">
&nbsp;&nbsp;&nbsp;
</td>
</tr>
<tr>
<td width="12" style="display: block">
&nbsp;&nbsp;&nbsp;
</td>
<td>
<table
border="0"
width="100%%"
cellspacing="0"
cellpadding="0"
style="border-collapse: collapse"
>
<tbody>
<tr>
<td></td>
<td
style="
margin-top: 10;
margin-left: 0;
margin-bottom: 10;
margin-right: 0;
color: #565a5c;
font-size: 18px;
"
>
<p
style="
margin-top: 10;
margin-left: 0;
margin-bottom: 10;
margin-right: 0;
color: #565a5c;
font-size: 18px;
"
>
Hi,
</p>
<p
style="
margin-top: 10;
margin-left: 0;
margin-bottom: 10;
margin-right: 0;
color: #565a5c;
font-size: 18px;
"
>
<!-- 이메일 -->
Someone tried to sign up for an Instagram
account with %s. If it
was you, enter this confirmation code in
the app:
</p>
</td>
</tr>
<tr>
<td></td>
<td
style="
padding: 10px;
color: #565a5c;
font-size: 32px;
font-weight: 500;
text-align: center;
padding-bottom: 25px;
"
>
<!-- 코드 -->
%s
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table
border="0"
cellspacing="0"
cellpadding="0"
style="
border-collapse: collapse;
margin: 0 auto 0 auto;
margin-top: 0;
margin-left: auto;
margin-bottom: 0;
margin-right: auto;
width: 100%%;
max-width: 600px;
"
>
<tbody style="text-align: center">
<tr>
<td width="15" style="display: block">
&nbsp;&nbsp;&nbsp;
</td>
</tr>
<tr>
<td>
<table style="padding-top: 10px; ">
<tbody style="margin: auto">
<tr>
<img
src="http://drive.google.com/uc?export=view&id=1Max2S-mP-GpssexCdfo2obS4OYpcSqK-"
height="26"
width="52"
alt="meta logo"
/>
</td>
</tr>
</tbody>
</table>
</tr>
<tr>
<td style="height: 10px"></td>
</tr>
<tr>
<td
style="
color: #abadae;
font-size: 11px;
padding-bottom: 5px;
"
>
© Instagram. Meta Platforms, Inc., 1601 Willow Road, Menlo
Park, CA 94025<br aria-hidden="true" />
</td>
</tr>
<tr>
<td
style="
color: #abadae;
font-size: 11px;
padding-bottom: 5px;
"
>
<!--이메일-->
This message was sent to %s.<br aria-hidden="true" />
</td>
</tr>
</td>
<td width="20" style="display: block">&nbsp;&nbsp;&nbsp;</td>
<td width="15"></td>
</tr>
<tr>
<td height="32" colspan="3" style="line-height: 32px">
&nbsp;
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td height="20" colspan="3" style="line-height: 20px">&nbsp;</td>
</tr>
</tbody>
</table>
</body>
</html>
Loading