funcionamiento

This commit is contained in:
Felipe
2025-02-06 17:50:26 -05:00
parent 3df5502026
commit 206aea72ed
+41 -18
View File
@@ -12,19 +12,19 @@
<h2 class="text-3xl font-bold text-gray-800 mb-6">Bienvenido</h2> <h2 class="text-3xl font-bold text-gray-800 mb-6">Bienvenido</h2>
<form id="loginForm" class="space-y-4"> <form id="loginForm" class="space-y-4">
<div class="relative"> <div class="relative">
<input type="email" id="email" placeholder="Email" class="w-full p-3 pl-10 border-2 border-gray-200 rounded-lg focus:outline-none focus:border-green-500 transition-all" required> <input type="text" id="username" placeholder="Usuario" class="w-full p-3 pl-10 border-2 border-gray-200 rounded-lg focus:outline-none focus:border-green-500 transition-all" required>
<svg class="w-5 h-5 text-gray-400 absolute left-3 top-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"> <svg class="w-5 h-5 text-gray-400 absolute left-3 top-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"></path> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"></path>
</svg> </svg>
</div> </div>
<div class="relative"> <div class="relative">
<input type="password" id="password" placeholder="Password" class="w-full p-3 pl-10 border-2 border-gray-200 rounded-lg focus:outline-none focus:border-green-500 transition-all" required> <input type="password" id="password" placeholder="Contraseña" class="w-full p-3 pl-10 border-2 border-gray-200 rounded-lg focus:outline-none focus:border-green-500 transition-all" required>
<svg class="w-5 h-5 text-gray-400 absolute left-3 top-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"> <svg class="w-5 h-5 text-gray-400 absolute left-3 top-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z"></path> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z"></path>
</svg> </svg>
</div> </div>
<button type="submit" class="w-full bg-gradient-to-br from-green-500 to-green-600 text-white p-3 rounded-lg font-semibold hover:from-green-600 hover:to-green-700 transition-all transform hover:scale-105"> <button type="submit" class="w-full bg-gradient-to-br from-green-500 to-green-600 text-white p-3 rounded-lg font-semibold hover:from-green-600 hover:to-green-700 transition-all transform hover:scale-105">
Login Iniciar sesión
</button> </button>
</form> </form>
<div class="mt-4 text-sm text-gray-600 hover:text-green-500 transition-all"> <div class="mt-4 text-sm text-gray-600 hover:text-green-500 transition-all">
@@ -36,24 +36,47 @@
document.getElementById('loginForm').addEventListener('submit', async function (e) { document.getElementById('loginForm').addEventListener('submit', async function (e) {
e.preventDefault(); e.preventDefault();
const email = document.getElementById('email').value; const username = document.getElementById('username').value;
const password = document.getElementById('password').value; const password = document.getElementById('password').value;
const response = await fetch('/api/login', { try {
method: 'POST', const response = await fetch('/do/login', {
headers: { method: 'POST',
'Content-Type': 'application/json', headers: {
}, 'Content-Type': 'application/json',
body: JSON.stringify({ email, password }), },
}); body: JSON.stringify({
nombre_usuario: username,
password: password,
}),
});
const result = await response.json(); const result = await response.json();
if (result.success) { switch (result.status) {
alert('Login exitoso'); case "success":
window.location.href = '/dashboard'; window.location.href = '/app/dashboard';
} else { break;
alert('Error: ' + result.message);
case "missing":
window.location.href = '/update-credentials';
break;
case "error":
if (result.message === "no_password") {
window.location.href = '/update-credentials';
} else {
alert("Error: " + result.message);
}
break;
default:
alert("Respuesta inesperada del servidor");
break;
}
} catch (error) {
console.error("Error en la solicitud:", error);
alert("Hubo un error al intentar iniciar sesión. Inténtalo de nuevo.");
} }
}); });
</script> </script>