feat: 이메일 인증 코드 재요청시 기존 코드 삭제 추가

This commit is contained in:
kgc91747 2024-08-07 17:19:07 +09:00
parent 9032a156c4
commit e9d0db30e5
5 changed files with 19 additions and 10 deletions

View File

@ -1,7 +1,6 @@
package com.edufocus.edufocus.mail.service; package com.edufocus.edufocus.mail.service;
import com.edufocus.edufocus.redis.util.RedisUtil; import com.edufocus.edufocus.redis.util.RedisUtil;
import com.edufocus.edufocus.user.model.entity.vo.User;
import com.edufocus.edufocus.user.model.repository.UserRepository; import com.edufocus.edufocus.user.model.repository.UserRepository;
import com.edufocus.edufocus.user.model.service.UserService; import com.edufocus.edufocus.user.model.service.UserService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -11,7 +10,6 @@ import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.NoSuchElementException;
import java.util.Random; import java.util.Random;
@Service @Service
@ -31,10 +29,16 @@ public class MailServiceImpl implements MailService {
@Override @Override
public void sendMail(String email) { public void sendMail(String email) {
String code = createRandomCode(); String code = createRandomCode();
redisUtil.setDataExpire(code, email, 60 * 5L);
SimpleMailMessage mail = createEmail(email, "[EDUFOCUS] 비밀번호 찾기 안내", code); if (redisUtil.exists(email)) {
mailSender.send(mail); redisUtil.deleteData(redisUtil.getData(email));
}
redisUtil.setDataExpire(code, email, 60 * 5L);
redisUtil.setDataExpire(email, code, 60 * 5L);
// SimpleMailMessage mail = createEmail(email, "[EDUFOCUS] 비밀번호 찾기 안내", code);
// mailSender.send(mail);
} }
@Override @Override
@ -46,6 +50,7 @@ public class MailServiceImpl implements MailService {
private SimpleMailMessage createEmail(String to, String title, String code) { private SimpleMailMessage createEmail(String to, String title, String code) {
SimpleMailMessage message = new SimpleMailMessage(); SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("EDUFOCUS");
message.setTo(to); message.setTo(to);
message.setSubject(title); message.setSubject(title);
message.setText("인증번호 6자리입니다 : " + code); message.setText("인증번호 6자리입니다 : " + code);

View File

@ -33,4 +33,8 @@ public class RedisUtil {
stringRedisTemplate.delete(key); stringRedisTemplate.delete(key);
} }
public boolean exists(String key) {
return stringRedisTemplate.hasKey(key);
}
} }

View File

@ -71,9 +71,9 @@ public class UserController {
// 비밀번호 찾기를 통한 변경 // 비밀번호 찾기를 통한 변경
@PutMapping("/updateforgottenpassword") @PutMapping("/updateforgottenpassword")
public ResponseEntity<String> updatePassword(@RequestParam long userId, public ResponseEntity<String> updatePassword(@RequestParam String email,
@RequestParam String newPassword) { @RequestParam String newPassword) {
userService.changeForgottenPassword(userId, newPassword); userService.changeForgottenPassword(email, newPassword);
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }

View File

@ -28,5 +28,5 @@ public interface UserService {
boolean isEmailExist(String email); boolean isEmailExist(String email);
void changeForgottenPassword(Long id, String newPassword); void changeForgottenPassword(String email, String newPassword);
} }

View File

@ -143,8 +143,8 @@ public class UserServiceImpl implements UserService {
} }
@Override @Override
public void changeForgottenPassword(Long id, String newPassword) { public void changeForgottenPassword(String email, String newPassword) {
User user = userRepository.findById(id).orElse(null); User user = userRepository.findByEmail(email).orElse(null);
if (user == null) { if (user == null) {
throw new UserException("User not found"); throw new UserException("User not found");