swimmingArt/public/js/login.js

30 lines
823 B
JavaScript

document.getElementById('loginForm').addEventListener('submit', async function (e) {
e.preventDefault();
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
const res = await fetch('/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password })
});
if (!res.ok) {
alert("Credenciales incorrectas.");
return;
}
const data = await res.json();
sessionStorage.setItem("userId", data.userId);
sessionStorage.setItem("role", data.role);
if (data.role === "coach") {
window.location.href = "coach.html";
} else if (data.role === "athlete") {
window.location.href = "atleta.html";
} else {
window.location.href = "ventanaPrincipal.html";
}
});