Files
PracticaENERGO2/auth.js

24 lines
803 B
JavaScript

document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById('auth-form');
const errorEl = document.getElementById('auth-error');
const isRegister = document.body.dataset.page === 'register';
form.addEventListener('submit', async (e) => {
e.preventDefault();
errorEl.textContent = '';
const email = document.getElementById('email').value.trim();
const password = document.getElementById('password').value;
const endpoint = isRegister ? '/api/auth/register' : '/api/auth/login';
try {
await API.request(endpoint, {
method: 'POST',
body: JSON.stringify({ email, password }),
});
window.location.href = '/';
} catch (err) {
errorEl.textContent = err.message;
}
});
});