mirror of
https://gitlab.com/etc404/software-engineering-project.git
synced 2026-05-10 20:52:58 +00:00
resend code added
This commit is contained in:
@@ -22,10 +22,16 @@ public class EmailController {
|
|||||||
|
|
||||||
|
|
||||||
@PostMapping("/send")
|
@PostMapping("/send")
|
||||||
public String send(@RequestParam String email) {
|
public ResponseEntity<?> send(@RequestParam String email) {
|
||||||
String otp = emailService.sendOtpEmail(email);
|
|
||||||
return "OTP sent to " + email + " (for demo, OTP: " + otp + ")";
|
if (!otpStore.canResend(email)) {
|
||||||
|
return ResponseEntity.status(429).body("Please wait before requesting another code.");
|
||||||
|
}
|
||||||
|
|
||||||
|
emailService.sendOtpEmail(email);
|
||||||
|
return ResponseEntity.ok().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@PostMapping("/verify")
|
@PostMapping("/verify")
|
||||||
public ResponseEntity<String> verify(@RequestParam String email, @RequestParam String otp) {
|
public ResponseEntity<String> verify(@RequestParam String email, @RequestParam String otp) {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ public class EmailService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private OtpStore otpStore;
|
private OtpStore otpStore;
|
||||||
|
|
||||||
public String sendOtpEmail(String toEmail) {
|
public void sendOtpEmail(String toEmail) {
|
||||||
String otp = OtpUtil.generateOtp(6);
|
String otp = OtpUtil.generateOtp(6);
|
||||||
otpStore.save(toEmail, otp);
|
otpStore.save(toEmail, otp);
|
||||||
|
|
||||||
@@ -23,16 +23,12 @@ public class EmailService {
|
|||||||
message.setSubject("Welcome to Thyme Crunch – Verify Your Account");
|
message.setSubject("Welcome to Thyme Crunch – Verify Your Account");
|
||||||
message.setText(
|
message.setText(
|
||||||
"Welcome to Thyme Crunch!\n\n" +
|
"Welcome to Thyme Crunch!\n\n" +
|
||||||
"Thank you for creating an account. To complete your registration, " +
|
|
||||||
"please enter the verification code below:\n\n" +
|
|
||||||
"Your verification code: " + otp + "\n\n" +
|
"Your verification code: " + otp + "\n\n" +
|
||||||
"This code will expire in 5 minutes. If you did not create an account " +
|
"This code will expire in 5 minutes.\n\n" +
|
||||||
"with Thyme Crunch, you can safely ignore this email.\n\n" +
|
"If you did not create an account, ignore this email.\n\n" +
|
||||||
"Happy cooking,\n" +
|
|
||||||
"The Thyme Crunch Team"
|
"The Thyme Crunch Team"
|
||||||
);
|
);
|
||||||
|
|
||||||
mailSender.send(message);
|
mailSender.send(message);
|
||||||
return otp;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,41 +1,63 @@
|
|||||||
package com.example.demo.service;
|
package com.example.demo.service;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class OtpStore {
|
public class OtpStore {
|
||||||
private final Map<String, String> otpMap = new HashMap<>();
|
|
||||||
private final Map<String, Long> expiryMap = new HashMap<>();
|
private final Map<String, String> otpMap = new ConcurrentHashMap<>();
|
||||||
private static final long EXPIRY_MS = 5 * 60 * 1000;
|
private final Map<String, Long> expiryMap = new ConcurrentHashMap<>();
|
||||||
|
private final Map<String, Long> lastSentMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
private static final long EXPIRY_MS = 5 * 60 * 1000; // 5 minutes
|
||||||
|
private static final long RESEND_COOLDOWN_MS = 60 * 1000; // 1 minute
|
||||||
|
|
||||||
|
|
||||||
public void save(String email, String otp) {
|
public void save(String email, String otp) {
|
||||||
otpMap.put(email, otp);
|
otpMap.put(email, otp);
|
||||||
expiryMap.put(email, System.currentTimeMillis() + EXPIRY_MS);
|
expiryMap.put(email, System.currentTimeMillis() + EXPIRY_MS);
|
||||||
|
lastSentMap.put(email, System.currentTimeMillis());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean validate(String email, String otp) {
|
public boolean validate(String email, String otp) {
|
||||||
if (!expiryMap.containsKey(email) || System.currentTimeMillis() > expiryMap.get(email)) {
|
Long expiry = expiryMap.get(email);
|
||||||
clear(email); // expired
|
|
||||||
|
if (expiry == null || System.currentTimeMillis() > expiry) {
|
||||||
|
clear(email);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return otp.equals(otpMap.get(email));
|
return otp.equals(otpMap.get(email));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean canResend(String email) {
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
|
long lastSent = lastSentMap.getOrDefault(email, 0L);
|
||||||
|
|
||||||
|
return (now - lastSent) >= RESEND_COOLDOWN_MS;
|
||||||
|
}
|
||||||
|
|
||||||
public void clear(String email) {
|
public void clear(String email) {
|
||||||
otpMap.remove(email);
|
otpMap.remove(email);
|
||||||
expiryMap.remove(email);
|
expiryMap.remove(email);
|
||||||
|
lastSentMap.remove(email);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Scheduled(fixedRate = 120000)
|
@Scheduled(fixedRate = 120000)
|
||||||
public void clearExpired() {
|
public void clearExpired() {
|
||||||
long now = System.currentTimeMillis();
|
long now = System.currentTimeMillis();
|
||||||
|
|
||||||
expiryMap.entrySet().removeIf(entry -> {
|
expiryMap.entrySet().removeIf(entry -> {
|
||||||
if (now > entry.getValue()) {
|
if (now > entry.getValue()) {
|
||||||
otpMap.remove(entry.getKey());
|
String email = entry.getKey();
|
||||||
|
otpMap.remove(email);
|
||||||
|
lastSentMap.remove(email);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -174,6 +174,14 @@ html {
|
|||||||
transition: background-color 0.1s ease, transform 0.2s ease;
|
transition: background-color 0.1s ease, transform 0.2s ease;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#resendBtn:disabled {
|
||||||
|
background-color: #5a2a2a;
|
||||||
|
color: #d8caa0;
|
||||||
|
cursor: not-allowed;
|
||||||
|
transform: none;
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
|
||||||
.login-box button:hover {
|
.login-box button:hover {
|
||||||
background-color: var(--dusty-red-hover);
|
background-color: var(--dusty-red-hover);
|
||||||
transform: scale(1.05);
|
transform: scale(1.05);
|
||||||
|
|||||||
@@ -5,7 +5,9 @@
|
|||||||
<meta name="_csrf_header" th:content="${_csrf.headerName}"/>
|
<meta name="_csrf_header" th:content="${_csrf.headerName}"/>
|
||||||
<meta name="_csrf" th:content="${_csrf.token}"/>
|
<meta name="_csrf" th:content="${_csrf.token}"/>
|
||||||
<title>Verify Your Thyme Crunch Account</title>
|
<title>Verify Your Thyme Crunch Account</title>
|
||||||
<link rel="stylesheet" th:href="@{css/create-account.css}">
|
|
||||||
|
<link rel="stylesheet" th:href="@{/css/login.css}">
|
||||||
|
|
||||||
<link href="https://fonts.googleapis.com/css2?family=Delius+Swash+Caps&family=Mali:ital,wght@0,200;0,300;0,400;0,500;0,600;0,700;1,200;1,300;1,400;1,500;1,600;1,700" rel="stylesheet">
|
<link href="https://fonts.googleapis.com/css2?family=Delius+Swash+Caps&family=Mali:ital,wght@0,200;0,300;0,400;0,500;0,600;0,700;1,200;1,300;1,400;1,500;1,600;1,700" rel="stylesheet">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@@ -13,15 +15,15 @@
|
|||||||
<div class="header-wrap">
|
<div class="header-wrap">
|
||||||
<a th:href="@{/explore}">
|
<a th:href="@{/explore}">
|
||||||
<header class="top-header">
|
<header class="top-header">
|
||||||
<img th:src="@{/images/header_left.png}" alt="Violin f-hole shape to the left of header." class="swirl">
|
<img th:src="@{/images/header_left.png}" class="swirl">
|
||||||
<h1 class="site-name">Thyme Crunch</h1>
|
<h1 class="site-name">Thyme Crunch</h1>
|
||||||
<img th:src="@{/images/header_right.png}" alt="Violin f-hole shape to the right of header." class="swirl">
|
<img th:src="@{/images/header_right.png}" class="swirl">
|
||||||
</header>
|
</header>
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="align-decor">
|
<div class="align-decor">
|
||||||
<img th:src="@{images/decor_left.png}" alt="" class="vert_swirl">
|
<img th:src="@{/images/decor_left.png}" class="vert_swirl">
|
||||||
|
|
||||||
<main class="main-content">
|
<main class="main-content">
|
||||||
<div class="login-box">
|
<div class="login-box">
|
||||||
@@ -32,19 +34,80 @@
|
|||||||
<label for="otpInput">Verification Code</label>
|
<label for="otpInput">Verification Code</label>
|
||||||
<input type="text" id="otpInput" maxlength="6" placeholder="000000" required>
|
<input type="text" id="otpInput" maxlength="6" placeholder="000000" required>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p id="message"></p>
|
<p id="message"></p>
|
||||||
|
|
||||||
<button onclick="verifyOtp()">Verify</button>
|
<button onclick="verifyOtp()">Verify</button>
|
||||||
|
|
||||||
|
<p class="resend-text">
|
||||||
|
Didn’t get the code?
|
||||||
|
<button id="resendBtn" onclick="resendOtp()" disabled>
|
||||||
|
Resend code (60s)
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
<img th:src="@{images/decor_right.png}" alt="" class="vert_swirl">
|
<img th:src="@{/images/decor_right.png}" class="vert_swirl">
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
const COOLDOWN_SECONDS = 60;
|
||||||
|
let countdownInterval = null;
|
||||||
|
|
||||||
|
function startCooldown() {
|
||||||
|
const btn = document.getElementById("resendBtn");
|
||||||
|
let remaining = COOLDOWN_SECONDS;
|
||||||
|
|
||||||
|
btn.disabled = true;
|
||||||
|
btn.textContent = `Resend code (${remaining}s)`;
|
||||||
|
|
||||||
|
countdownInterval = setInterval(() => {
|
||||||
|
remaining--;
|
||||||
|
btn.textContent = `Resend code (${remaining}s)`;
|
||||||
|
|
||||||
|
if (remaining <= 0) {
|
||||||
|
clearInterval(countdownInterval);
|
||||||
|
btn.disabled = false;
|
||||||
|
btn.textContent = "Resend code";
|
||||||
|
}
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function resendOtp() {
|
||||||
|
const email = sessionStorage.getItem("pendingEmail");
|
||||||
|
const message = document.getElementById("message");
|
||||||
|
|
||||||
|
const csrfToken = document.querySelector('meta[name="_csrf"]').getAttribute('content');
|
||||||
|
const csrfHeader = document.querySelector('meta[name="_csrf_header"]').getAttribute('content');
|
||||||
|
|
||||||
|
startCooldown();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`/api/email/send?email=${encodeURIComponent(email)}`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: { [csrfHeader]: csrfToken }
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
message.style.color = "green";
|
||||||
|
message.textContent = "A new code has been sent.";
|
||||||
|
} else {
|
||||||
|
message.style.color = "red";
|
||||||
|
message.textContent = "Couldn't resend code.";
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
message.style.color = "red";
|
||||||
|
message.textContent = "Network error.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window.addEventListener("DOMContentLoaded", startCooldown);
|
||||||
|
|
||||||
async function verifyOtp() {
|
async function verifyOtp() {
|
||||||
const otp = document.getElementById("otpInput").value;
|
const otp = document.getElementById("otpInput").value;
|
||||||
const email = sessionStorage.getItem("pendingEmail");
|
const email = sessionStorage.getItem("pendingEmail");
|
||||||
const pendingUser = JSON.parse(sessionStorage.getItem("pendingUser"));
|
const pendingUser = JSON.parse(sessionStorage.getItem("pendingUser"));
|
||||||
|
const message = document.getElementById("message");
|
||||||
|
|
||||||
const csrfToken = document.querySelector('meta[name="_csrf"]').getAttribute('content');
|
const csrfToken = document.querySelector('meta[name="_csrf"]').getAttribute('content');
|
||||||
const csrfHeader = document.querySelector('meta[name="_csrf_header"]').getAttribute('content');
|
const csrfHeader = document.querySelector('meta[name="_csrf_header"]').getAttribute('content');
|
||||||
@@ -69,11 +132,11 @@ async function verifyOtp() {
|
|||||||
sessionStorage.removeItem("pendingUser");
|
sessionStorage.removeItem("pendingUser");
|
||||||
|
|
||||||
message.style.color = "green";
|
message.style.color = "green";
|
||||||
message.textContent = "Account verified! Redirecting to login...";
|
message.textContent = "Account verified! Redirecting...";
|
||||||
setTimeout(() => window.location.href = "/login", 1500);
|
setTimeout(() => window.location.href = "/login", 1500);
|
||||||
} else {
|
} else {
|
||||||
message.style.color = "red";
|
message.style.color = "red";
|
||||||
message.textContent = "Invalid or expired code. Please try again.";
|
message.textContent = "Invalid or expired code.";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user