42 lines
1.3 KiB
JavaScript
42 lines
1.3 KiB
JavaScript
// Login
|
|
const loginForm = document.getElementById('loginForm');
|
|
const registerForm = document.getElementById('registerForm');
|
|
const authMsg = document.getElementById('authMsg');
|
|
|
|
if (loginForm) {
|
|
loginForm.addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
const formData = Object.fromEntries(new FormData(loginForm));
|
|
const res = await fetch('/api/login', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(formData)
|
|
});
|
|
const data = await res.json();
|
|
if (data.success) {
|
|
authMsg.textContent = 'Login successful!';
|
|
setTimeout(() => window.location = 'index.html', 800);
|
|
} else {
|
|
authMsg.textContent = data.error || 'Login failed.';
|
|
}
|
|
});
|
|
}
|
|
|
|
if (registerForm) {
|
|
registerForm.addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
const formData = Object.fromEntries(new FormData(registerForm));
|
|
const res = await fetch('/api/register', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(formData)
|
|
});
|
|
const data = await res.json();
|
|
if (data.success) {
|
|
authMsg.textContent = 'Registration successful! Please login.';
|
|
} else {
|
|
authMsg.textContent = data.error || 'Registration failed.';
|
|
}
|
|
});
|
|
}
|