mirror of
https://github.com/marcogll/ap_pos.git
synced 2026-01-13 13:15:16 +00:00
44 lines
1.5 KiB
JavaScript
44 lines
1.5 KiB
JavaScript
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
const loginForm = document.getElementById('loginForm');
|
|
const errorMessage = document.getElementById('error-message');
|
|
|
|
// Redirigir si ya está autenticado
|
|
fetch('/api/check-auth')
|
|
.then(res => res.json())
|
|
.then(data => {
|
|
if (data.isAuthenticated) {
|
|
window.location.href = '/';
|
|
}
|
|
});
|
|
|
|
loginForm.addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
errorMessage.style.display = 'none';
|
|
|
|
const username = document.getElementById('username').value;
|
|
const password = document.getElementById('password').value;
|
|
|
|
try {
|
|
const response = await fetch('/api/login', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ username, password }),
|
|
});
|
|
|
|
if (response.ok) {
|
|
window.location.href = '/'; // Redirigir a la página principal
|
|
} else {
|
|
const errorData = await response.json();
|
|
errorMessage.textContent = errorData.error || 'Error al iniciar sesión.';
|
|
errorMessage.style.display = 'block';
|
|
}
|
|
} catch (error) {
|
|
errorMessage.textContent = 'No se pudo conectar con el servidor.';
|
|
errorMessage.style.display = 'block';
|
|
}
|
|
});
|
|
});
|