mirror of
https://github.com/marcogll/ap_pos.git
synced 2026-01-13 21:25:16 +00:00
feat: Add initial setup page and functionality for admin account creation
- Created setup.html for the initial configuration of the admin account. - Implemented setup.js to handle form submission and validation. - Added logo images for branding. - Introduced storage.js for API data handling (load, save, remove). - Added styles.css for consistent styling across the application.
This commit is contained in:
46
login.js
Normal file
46
login.js
Normal file
@@ -0,0 +1,46 @@
|
||||
|
||||
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) {
|
||||
const data = await response.json();
|
||||
// Guardar el rol del usuario para usarlo en la app principal
|
||||
sessionStorage.setItem('userRole', data.role);
|
||||
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';
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user