-
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.
- Loading branch information
Showing
12 changed files
with
382 additions
and
302 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
78 changes: 6 additions & 72 deletions
78
src/main/java/umc/kkijuk/server/member/emailauth/MailService.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 |
---|---|---|
@@ -1,78 +1,12 @@ | ||
package umc.kkijuk.server.member.emailauth; | ||
|
||
import jakarta.mail.MessagingException; | ||
import jakarta.mail.internet.MimeMessage; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.stereotype.Service; | ||
import umc.kkijuk.server.common.domian.exception.CertificationNumberMismatchException; | ||
import umc.kkijuk.server.common.domian.exception.MemberAlreadyExistsException; | ||
import umc.kkijuk.server.member.repository.MemberJpaRepository; | ||
import umc.kkijuk.server.member.dto.MemberEmailDto; | ||
|
||
import java.util.Random; | ||
public interface MailService { | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class MailService { | ||
MailCertificationResponse sendMail(String mail); | ||
|
||
private final MailCertification mailCertification; | ||
private final JavaMailSender javaMailSender; | ||
private final MemberJpaRepository memberJpaRepository; | ||
Boolean verifyMail(MailCertificationDto requestDto); | ||
|
||
@Value("${spring.mail.username}") | ||
private String senderEmail; | ||
|
||
|
||
private String createRandomNumber() { | ||
Random rand = new Random(); | ||
String randomNum = ""; | ||
for (int i = 0; i < 6; i++) { | ||
String random = Integer.toString(rand.nextInt(10)); | ||
randomNum += random; | ||
} | ||
|
||
return randomNum; | ||
} | ||
|
||
public MailCertificationResponse sendMail(String mail) { | ||
if(memberJpaRepository.findByEmail(mail).isPresent()){ | ||
throw new MemberAlreadyExistsException(); | ||
} | ||
|
||
MimeMessage message = javaMailSender.createMimeMessage(); | ||
String randomNum = createRandomNumber(); | ||
|
||
try { | ||
message.setFrom(senderEmail); | ||
message.setRecipients(MimeMessage.RecipientType.TO, mail); | ||
message.setSubject("이메일 인증"); | ||
String body = ""; | ||
body += "<h3>" + "요청하신 인증 번호입니다." + "</h3>"; | ||
body += "<h1>" + randomNum + "</h1>"; | ||
body += "<h3>" + "감사합니다." + "</h3>"; | ||
message.setText(body,"UTF-8", "html"); | ||
} catch (MessagingException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
javaMailSender.send(message); | ||
mailCertification.createMailCertification(mail,randomNum); | ||
return new MailCertificationResponse(mail, randomNum); | ||
} | ||
|
||
// 인증 번호 검증 | ||
public Boolean verifyMail(MailCertificationDto requestDto) { | ||
if (isVerify(requestDto)) { | ||
throw new CertificationNumberMismatchException(); | ||
} | ||
mailCertification.deleteMailCertification(requestDto.getEmail()); | ||
|
||
return true; | ||
} | ||
private boolean isVerify(MailCertificationDto requestDto) { | ||
return !(mailCertification.hasKey(requestDto.getEmail()) && | ||
mailCertification.getMailCertification(requestDto.getEmail()) | ||
.equals(requestDto.getAuthNumber())); | ||
} | ||
} | ||
Boolean confirmDupEmail(MemberEmailDto memberEmailDto); | ||
} |
91 changes: 91 additions & 0 deletions
91
src/main/java/umc/kkijuk/server/member/emailauth/MailServiceImpl.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,91 @@ | ||
package umc.kkijuk.server.member.emailauth; | ||
|
||
import jakarta.mail.MessagingException; | ||
import jakarta.mail.internet.MimeMessage; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.stereotype.Service; | ||
import umc.kkijuk.server.common.domian.exception.CertificationNumberMismatchException; | ||
import umc.kkijuk.server.common.domian.exception.MemberAlreadyExistsException; | ||
import umc.kkijuk.server.member.domain.Member; | ||
import umc.kkijuk.server.member.dto.MemberEmailDto; | ||
import umc.kkijuk.server.member.repository.MemberRepository; | ||
|
||
import java.util.Optional; | ||
import java.util.Random; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class MailServiceImpl implements MailService{ | ||
|
||
private final MailCertification mailCertification; | ||
private final JavaMailSender javaMailSender; | ||
private final MemberRepository memberRepository; | ||
|
||
@Value("${spring.mail.username}") | ||
private String senderEmail; | ||
|
||
|
||
private String createRandomNumber() { | ||
Random rand = new Random(); | ||
String randomNum = ""; | ||
for (int i = 0; i < 6; i++) { | ||
String random = Integer.toString(rand.nextInt(10)); | ||
randomNum += random; | ||
} | ||
|
||
return randomNum; | ||
} | ||
|
||
public MailCertificationResponse sendMail(String mail) { | ||
if(memberRepository.findByEmail(mail).isPresent()){ | ||
throw new MemberAlreadyExistsException(); | ||
} | ||
|
||
MimeMessage message = javaMailSender.createMimeMessage(); | ||
String randomNum = createRandomNumber(); | ||
|
||
try { | ||
message.setFrom(senderEmail); | ||
message.setRecipients(MimeMessage.RecipientType.TO, mail); | ||
message.setSubject("이메일 인증"); | ||
String body = ""; | ||
body += "<h3>" + "요청하신 인증 번호입니다." + "</h3>"; | ||
body += "<h1>" + randomNum + "</h1>"; | ||
body += "<h3>" + "감사합니다." + "</h3>"; | ||
message.setText(body,"UTF-8", "html"); | ||
} catch (MessagingException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
javaMailSender.send(message); | ||
mailCertification.createMailCertification(mail,randomNum); | ||
return new MailCertificationResponse(mail, randomNum); | ||
} | ||
|
||
// 인증 번호 검증 | ||
public Boolean verifyMail(MailCertificationDto requestDto) { | ||
if (isVerify(requestDto)) { | ||
throw new CertificationNumberMismatchException(); | ||
} | ||
mailCertification.deleteMailCertification(requestDto.getEmail()); | ||
|
||
return true; | ||
} | ||
private Boolean isVerify(MailCertificationDto requestDto) { | ||
return !(mailCertification.hasKey(requestDto.getEmail()) && | ||
mailCertification.getMailCertification(requestDto.getEmail()) | ||
.equals(requestDto.getAuthNumber())); | ||
} | ||
|
||
@Override | ||
public Boolean confirmDupEmail(MemberEmailDto memberEmailDto) { | ||
Optional<Member> member = memberRepository.findByEmail(memberEmailDto.getEmail()); | ||
if(member.isEmpty()){ | ||
return true; | ||
}else{ | ||
return false; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/umc/kkijuk/server/member/repository/MemberRepository.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,11 @@ | ||
package umc.kkijuk.server.member.repository; | ||
|
||
import umc.kkijuk.server.member.domain.Member; | ||
|
||
import java.util.Optional; | ||
|
||
public interface MemberRepository { | ||
Optional<Member> findById(Long id); | ||
Optional<Member> findByEmail(String email); | ||
Member save(Member member); | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/umc/kkijuk/server/member/repository/MemberRepositoryImpl.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 umc.kkijuk.server.member.repository; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
import umc.kkijuk.server.member.domain.Member; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class MemberRepositoryImpl implements MemberRepository{ | ||
private final MemberJpaRepository memberJpaRepository; | ||
|
||
@Override | ||
public Optional<Member> findById(Long id) { | ||
return memberJpaRepository.findById(id); | ||
} | ||
|
||
@Override | ||
public Optional<Member> findByEmail(String email) { | ||
return memberJpaRepository.findByEmail(email); | ||
} | ||
|
||
@Override | ||
public Member save(Member member) { | ||
return memberJpaRepository.save(member); | ||
} | ||
} |
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
Oops, something went wrong.