invoice-processing-google-d.../templates/login.html
2025-08-26 12:26:03 +02:00

92 lines
4.5 KiB
HTML

<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Iniciar Sesión - ACME Invoice Processor</title>
<style>
body { font-family: sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #f4f4f9; }
.login-container { padding: 2rem; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); width: 300px; }
h1 { text-align: center; }
.form-group { margin-bottom: 1rem; }
label { display: block; margin-bottom: 0.5rem; }
input { width: 100%; padding: 0.5rem; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; }
button { width: 100%; padding: 0.7rem; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; }
button:hover { background-color: #0056b3; }
p { text-align: center; }
.message { text-align: center; padding: 1rem; margin-top: 1rem; border-radius: 4px; display: none; }
.error { background-color: #f8d7da; color: #721c24; }
</style>
</head>
<body>
<div class="login-container">
<h1>Iniciar Sesión</h1>
<form id="login-form">
<div class="form-group">
<label for="username">Nombre de Usuario</label>
<input type="text" id="username" name="username" required>
</div>
<div class="form-group">
<label for="password">Contraseña</label>
<input type="password" id="password" name="password" required>
</div>
<!-- Le damos un id al botón para poder seleccionarlo -->
<button type="submit" id="login-button">Entrar</button>
</form>
<p><a href="/register">¿No tienes una cuenta? Regístrate</a></p>
<div id="message-container" class="message"></div>
</div>
<!-- ======================= SCRIPT FINAL CORREGIDO ======================= -->
<script>
console.log("Página de login cargada. El script se está ejecutando.");
const form = document.getElementById('login-form');
const button = document.getElementById('login-button'); // Seleccionamos el botón
const messageContainer = document.getElementById('message-container');
if (form && button) {
console.log("Formulario y botón encontrados. Adjuntando listener al BOTÓN...");
// Cambiamos el listener para que escuche el evento 'click' del botón
button.addEventListener('click', async (event) => {
event.preventDefault(); // ¡Muy importante para que no recargue la página!
console.log("¡EVENTO CLICK CAPTURADO! Iniciando llamada a la API...");
const username = form.username.value;
const password = form.password.value;
const formData = new URLSearchParams();
formData.append('username', username);
formData.append('password', password);
try {
const response = await fetch('/api/users/token', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: formData,
});
const data = await response.json();
if (response.ok) {
console.log("LOGIN EXITOSO. Token:", data.access_token);
localStorage.setItem('accessToken', data.access_token);
window.location.href = '/dashboard';
} else {
console.error("ERROR DE API. Respuesta:", data);
messageContainer.textContent = `Error: ${data.detail || 'Credenciales incorrectas.'}`;
messageContainer.className = 'message error';
messageContainer.style.display = 'block';
}
} catch (error) {
console.error("ERROR DE RED:", error);
messageContainer.textContent = 'Error de conexión con el servidor.';
messageContainer.className = 'message error';
messageContainer.style.display = 'block';
}
});
} else {
console.error("¡ERROR CRÍTICO! No se encontró el formulario o el botón.");
}
</script>
</body>
</html>