Skip to content

Commit

Permalink
modify send to email function (#98)
Browse files Browse the repository at this point in the history
* modify send to email function

* done countDown for resend to mail

* done countDown for resend to mail
  • Loading branch information
QuyDang1108 authored Sep 18, 2024
1 parent 3586155 commit 1eef291
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ EMAIL_SERVER=tranquangg.1108@gmail.com
PASSWORD_EMAIL_SERVER=qsnt yxbi yjud fcpf
RECAPTCHA_SECRET_KEY=6LfblD0qAAAAAO2lT_e5JwmxUFJ55uCpxPrdwk0T
RECAPTCHA_SITE_KEY=6LfblD0qAAAAAC1k-Zd8GfiyXmKjNWBV4xMC1BNP
CAPTCHA_ENABLE=false
CAPTCHA_ENABLE=false
TIMING_SEND_EMAIL=30000
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public class AuthController {
@Value("${default.captcha-enable}")
private String captchaEnable;

@Value("${default.timing-send-mail}")
private Integer timer;

private final UserService userService;
private final EmailService emailService;
private final ReCaptchaService reCaptchaService;
Expand Down Expand Up @@ -95,7 +98,7 @@ public ModelAndView indexForgotPassword() {
return modelAndView;
}

@PostMapping("/sendToEmail")
@PostMapping("/forgotPassword")
public ModelAndView doSendCodeToEmail(@ModelAttribute(Constants.ApiConstant.EMAIL_OBJECT)
String email) {
ModelAndView modelAndView = new ModelAndView();
Expand All @@ -106,6 +109,7 @@ public ModelAndView doSendCodeToEmail(@ModelAttribute(Constants.ApiConstant.EMAI

modelAndView.setViewName(Constants.ApiConstant.VERIFICATION_BY_EMAIL_VIEW);
modelAndView.addObject(Constants.ApiConstant.ERROR_MESSAGE_OBJECT, message);
modelAndView.addObject("timer", timer);

return modelAndView;
}
Expand Down
28 changes: 15 additions & 13 deletions src/main/java/com/fjb/sunrise/services/impl/EmailServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class EmailServiceImpl implements EmailService {
@Value("${spring.mail.username}")
private String emailServer;

@Value("${default.timing-send-mail}")
private Integer time;

private final UserRepository userRepository;
private final Encoder encoder;

Expand All @@ -41,19 +44,18 @@ public String sendEmail(VerificationByEmail verification) {
user.setVerificationCode(code);
userRepository.save(user);

SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(emailServer);
simpleMailMessage.setTo(verification.getEmail());
simpleMailMessage.setSubject("Verification Email");
simpleMailMessage.setText("Click this link to change password: \n"
+ "http://localhost:8086/sun/auth/verify?code="
+ code);

try {
Thread thread = new Thread(() -> {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(emailServer);
simpleMailMessage.setTo(verification.getEmail());
simpleMailMessage.setSubject("Verification Email");
simpleMailMessage.setText("Click this link to change password: \n"
+ "http://localhost:8086/sun/auth/verify?code="
+ code);
javaMailSender.send(simpleMailMessage);
} catch (Exception e) {
return "Gửi mail không thành công!";
}
});
thread.start();

return "Gửi mail thành công! \nVui lòng kiểm tra email của bạn!";
}

Expand All @@ -79,7 +81,7 @@ public String checkCode(String code) {
return "Lỗi trong quá trình xác thực!";
}

if (verification.getRequestTime().plusMinutes(2).isBefore(LocalDateTime.now())) {
if (verification.getRequestTime().plusSeconds(time).isBefore(LocalDateTime.now())) {
return "Vượt quá thời gian xác thực!";
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ default:
type-encode: ${TYPE_ENCODE}
recaptcha-secret-key: ${RECAPTCHA_SECRET_KEY}
recaptcha-site-key: ${RECAPTCHA_SITE_KEY}
captcha-enable: ${CAPTCHA_ENABLE}
captcha-enable: ${CAPTCHA_ENABLE}
timing-send-mail: ${TIMING_SEND_EMAIL}
2 changes: 1 addition & 1 deletion src/main/resources/templates/auth/changePassword.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ <h1 class="modal-title fs-5">NHẮC NHỞ</h1>
<div class="box-custom forgot">
<h2>Thay đổi mật khẩu cho tài khoản của bạn!</h2>
<form th:method="POST" th:action="@{/auth/changePassword}">
<input type="text" name="email" th:value="${email}" class="field-custom mb-3"/>
<input type="text" name="email" th:value="${email}" class="field-custom mb-3" readonly/>
<input type="text" name="newPassword" th:value="${newPassword}" class="field-custom mb-3" placeholder="Nhập password"/>
<button type="submit" >Gửi</button>
</form>
Expand Down
39 changes: 37 additions & 2 deletions src/main/resources/templates/auth/verificationByEmail.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,48 @@ <h1 class="modal-title fs-5">NHẮC NHỞ</h1>
<div class="background-custom">
<div class="box-custom forgot">
<h2>Điền email để xác thực tài khoản!</h2>
<form th:method="POST" th:action="@{/auth/sendToEmail}">
<form th:method="POST" th:action="@{/auth/forgotPassword}">
<input id="email" th:value="${email}" name="email" type="text" class="field-custom mb-3" placeholder="Nhập Email">
<button type="submit">Gửi</button>
<th:block th:if="${timer != null}">
<div id="timer-resend"></div>
</th:block>
<button id="btn-submit" type="submit">Gửi</button>
</form>
</div>
</div>

<script type="text/javascript" th:src="@{/js/bootstrap.bundle.min.js}"></script>
<script type="text/javascript">
const timeString = '[[${timer}]]';

window.onload = function() {
countDown();
};

function countDown() {
let time = +timeString/1000;
const timeView = document.getElementById("timer-resend");
const btn = document.getElementById("btn-submit");

const interval = setInterval(function() {
if (time > 0) {
timeView.innerHTML = `<p style="text-align: center; color: red">${renderTime(time)}</p>`;
time--;
btn.type = "button";
} else {
clearInterval(interval);
timeView.innerHTML = `<p style="text-align: center; color: red">Bạn chưa nhận được email?<p>`;
btn.type = "submit";
btn.textContent = "Gửi lại";
}
}, 1000);
}

function renderTime(second) {
const m = Math.floor(second / 60);
const s = second % 60;
return (m < 10 ? `0${m}` : `${m}`) + " : " + (s < 10 ? `0${s}` : `${s}`);
}
</script>
</body>
</html>
3 changes: 2 additions & 1 deletion src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ default:
recaptcha-site-key: efgh #gitleaks:allow
encode-key: abcdefghabcdefgh #gitleaks:allow
type-encode: AES #gitleaks:allow
captcha-enable: false
captcha-enable: false
timing-send-mail: 30000

0 comments on commit 1eef291

Please sign in to comment.