Skip to content

Commit

Permalink
feat : 이메일 인증 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
jyp-on committed Jul 1, 2024
1 parent bc26ab7 commit fa9f590
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.hallym.rehab.domain.user.controller;

import com.hallym.rehab.domain.user.dto.EmailDTO;
import com.hallym.rehab.domain.user.service.SmtpGmailSenderImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/email")
@RequiredArgsConstructor
public class SmtpController {
private final SmtpGmailSenderImpl senderService;

@GetMapping("/send-code/{email}")
public ResponseEntity<String> sendEmail(@PathVariable String email) {
senderService.sendEmail(email);
return ResponseEntity.ok("Email sent");
}

@PostMapping(value = "/verify-code/{email}", consumes = "application/json")
public ResponseEntity<String> sendEmailAndCode(@PathVariable String email, @RequestBody EmailDTO dto) {
if (senderService.verifyAuthCode(email, dto.getAuthCode())) {
return ResponseEntity.ok("Email verified");
}
return ResponseEntity.badRequest().body("Email verification failed");
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/hallym/rehab/domain/user/dto/EmailDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.hallym.rehab.domain.user.dto;

import lombok.Data;

@Data
public class EmailDTO {
private String authCode;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.hallym.rehab.domain.user.service;

import com.hallym.rehab.global.util.RedisUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
@RequiredArgsConstructor
public class SmtpGmailSenderImpl implements SmtpGmailSenderService{
private final JavaMailSender emailSender;
private final RedisUtil redisUtil;
@Value("${spring.mail.username}")
private String from;

@Override
public void sendEmail(String toEmail) {
if(redisUtil.existData(toEmail)) {
redisUtil.deleteData(toEmail);
}

String cipher = createCipher();
redisUtil.setDataExpire(toEmail, cipher, 60 * 5L);

SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(toEmail);
message.setSubject("이메일 인증번호를 알려드립니다.");
message.setText("5분안에 입력해야 합니다. \n" + "인증번호 : " + cipher);

emailSender.send(message);
}

@Override
public Boolean verifyAuthCode(String email, String authCode) {
String authCodeFoundByEmail = redisUtil.getData(email);
if (authCodeFoundByEmail == null) {
return false;
}
return authCodeFoundByEmail.equals(authCode);
}

@Override
public String createCipher() {
int leftLimit = 48; // number '0'
int rightLimit = 122; // alphabet 'z'ß
int targetStringLength = 6;
Random random = new Random();

return random.ints(leftLimit, rightLimit + 1)
.filter(i -> (i <=57 || i >=65) && (i <= 90 || i>= 97))
.limit(targetStringLength)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.hallym.rehab.domain.user.service;

public interface SmtpGmailSenderService {
void sendEmail(String toEmail);
Boolean verifyAuthCode(String email, String authCode);
String createCipher();
}

0 comments on commit fa9f590

Please sign in to comment.