PengLogin/public/scripts/register.js
2025-06-13 23:25:11 +02:00

35 lines
1.0 KiB
JavaScript

const registerForm = document.getElementById('register-form');
registerForm.addEventListener('submit', async (event) => {
event.preventDefault();
const formData = new FormData(registerForm);
const data = Object.fromEntries(formData.entries());
if (data.password !== data.confirmPassword) {
alert('Passwords do not match. Please try again.');
return;
}
try {
const response = await fetch('/api/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
if (response.ok) {
alert('Mail send, verify your mail.');
window.location.href = '/login'; // Redirect to login page
} else {
const errorData = await response.json();
alert(`Registration failed: ${errorData.message}`);
}
} catch (error) {
console.error('Error during registration:', error);
alert('An error occurred while trying to register.');
}
}
);