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 = "equipoDisponibles.html";
  } else if (data.role === "athlete") {
    window.location.href = "atleta.html";
  } else {
    window.location.href = "ventanaPrincipal.html";
  }
  
});
