mirror of
https://gitlab.com/etc404/software-engineering-project.git
synced 2026-05-10 20:52:58 +00:00
added more checks to email verif
This commit is contained in:
@@ -20,16 +20,22 @@ public class EmailController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private EmailService emailService;
|
private EmailService emailService;
|
||||||
|
|
||||||
|
@PostMapping("/check")
|
||||||
|
public ResponseEntity<?> check(@RequestParam String email){
|
||||||
|
if (emailService.isValidEmail(email) == false) {
|
||||||
|
return ResponseEntity.status(429).body("Invalid Email Detected.");
|
||||||
|
}
|
||||||
|
return ResponseEntity.ok().build();
|
||||||
|
}
|
||||||
|
|
||||||
@PostMapping("/send")
|
@PostMapping("/send")
|
||||||
public ResponseEntity<?> send(@RequestParam String email) {
|
public ResponseEntity<?> send(@RequestParam String email) {
|
||||||
|
try {
|
||||||
if (!otpStore.canResend(email)) {
|
|
||||||
return ResponseEntity.status(429).body("Please wait before requesting another code.");
|
|
||||||
}
|
|
||||||
|
|
||||||
emailService.sendOtpEmail(email);
|
emailService.sendOtpEmail(email);
|
||||||
return ResponseEntity.ok().build();
|
return ResponseEntity.ok().build();
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
return ResponseEntity.status(500).body("Failed to send verification email.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,18 @@
|
|||||||
package com.example.demo.service;
|
package com.example.demo.service;
|
||||||
|
|
||||||
|
import java.util.Hashtable;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.mail.MailException;
|
||||||
import org.springframework.mail.SimpleMailMessage;
|
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 javax.naming.directory.Attributes;
|
||||||
|
import javax.naming.directory.DirContext;
|
||||||
|
import javax.naming.directory.InitialDirContext;
|
||||||
|
|
||||||
|
import jakarta.mail.internet.AddressException;
|
||||||
|
import jakarta.mail.internet.InternetAddress;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class EmailService {
|
public class EmailService {
|
||||||
@@ -29,6 +38,45 @@ public class EmailService {
|
|||||||
"The Thyme Crunch Team"
|
"The Thyme Crunch Team"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
mailSender.send(message);
|
mailSender.send(message);
|
||||||
|
} catch (MailException e) {
|
||||||
|
otpStore.clear(toEmail);
|
||||||
|
throw new RuntimeException("Failed to send email to: " + toEmail, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isValidEmail(String email) {
|
||||||
|
if (email == null || email.isBlank()) return false;
|
||||||
|
|
||||||
|
String emailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$";
|
||||||
|
if (!email.matches(emailRegex)) return false;
|
||||||
|
|
||||||
|
try {
|
||||||
|
InternetAddress emailAddr = new InternetAddress(email, true);
|
||||||
|
emailAddr.validate();
|
||||||
|
} catch (AddressException ex) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
String domain = email.substring(email.indexOf('@') + 1);
|
||||||
|
return hasMxRecord(domain);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private boolean hasMxRecord(String domain) {
|
||||||
|
try {
|
||||||
|
Hashtable<String, String> env = new Hashtable<>();
|
||||||
|
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
|
||||||
|
env.put("java.naming.provider.url", "dns:");
|
||||||
|
env.put("com.sun.jndi.dns.timeout.initial", "2000");
|
||||||
|
env.put("com.sun.jndi.dns.timeout.retries", "1");
|
||||||
|
|
||||||
|
DirContext ctx = new InitialDirContext(env);
|
||||||
|
Attributes attrs = ctx.getAttributes(domain, new String[]{"MX"});
|
||||||
|
return attrs.get("MX") != null;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@@ -146,6 +146,18 @@ function isProfane(str) {
|
|||||||
const csrfHeader = document.querySelector('meta[name="_csrf_header"]').getAttribute('content');
|
const csrfHeader = document.querySelector('meta[name="_csrf_header"]').getAttribute('content');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const checkResponse = await fetch(`/api/email/check?email=${encodeURIComponent(email)}`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: { [csrfHeader]: csrfToken }
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!checkResponse.ok) {
|
||||||
|
const errorText = await checkResponse.text();
|
||||||
|
passwordError.style.color = "red";
|
||||||
|
passwordError.textContent = errorText || "Invalid Email Detected.";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
sessionStorage.setItem("pendingEmail", email);
|
sessionStorage.setItem("pendingEmail", email);
|
||||||
sessionStorage.setItem("pendingUser", JSON.stringify({
|
sessionStorage.setItem("pendingUser", JSON.stringify({
|
||||||
username: name,
|
username: name,
|
||||||
@@ -154,11 +166,17 @@ function isProfane(str) {
|
|||||||
role: "ROLE_USER"
|
role: "ROLE_USER"
|
||||||
}));
|
}));
|
||||||
|
|
||||||
await fetch(`/api/email/send?email=${encodeURIComponent(email)}`, {
|
const sendResponse = await fetch(`/api/email/send?email=${encodeURIComponent(email)}`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { [csrfHeader]: csrfToken }
|
headers: { [csrfHeader]: csrfToken }
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!sendResponse.ok) {
|
||||||
|
passwordError.style.color = "red";
|
||||||
|
passwordError.textContent = "Failed to send verification email. Please check your address.";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
passwordError.style.color = "green";
|
passwordError.style.color = "green";
|
||||||
passwordError.textContent = "Check your email for a verification code...";
|
passwordError.textContent = "Check your email for a verification code...";
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
@@ -171,6 +189,6 @@ function isProfane(str) {
|
|||||||
console.error("Request error:", error);
|
console.error("Request error:", error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
Reference in New Issue
Block a user