main
js 40 lines 1.55 KB
Raw
1 document.addEventListener('DOMContentLoaded', async () => {
2 await Auth.redirectIfAuthenticated();
3
4 document.getElementById('link-aviso-login')?.addEventListener('click', () => AvisoPrivacidad.mostrar('Aviso de Privacidad'));
5
6 document.getElementById('login-form').addEventListener('submit', async (e) => {
7 e.preventDefault();
8 Utils.clearAlert('alert-container');
9
10 const email = document.getElementById('email').value.trim();
11 const password = document.getElementById('password').value;
12
13 if (!email || !password) {
14 Utils.showAlert('alert-container', 'Ingresa tu correo y contraseña.');
15 return;
16 }
17
18 const submitBtn = e.target.querySelector('button[type="submit"]');
19 submitBtn.disabled = true;
20 submitBtn.textContent = 'Iniciando sesión...';
21
22 try {
23 const response = await API.request('/auth/login', {
24 method: 'POST',
25 body: JSON.stringify({ email, contrasena: password })
26 });
27
28 Auth.saveSession(response.token, response.usuario);
29 try {
30 localStorage.removeItem('bienvenida-pendiente');
31 sessionStorage.setItem('bienvenida-pendiente', '1');
32 } catch (e) {}
33 window.location.href = Auth.resolvePath('pages/inicio.html');
34 } catch (error) {
35 Utils.showAlert('alert-container', error.message || 'No se pudo iniciar sesión.');
36 submitBtn.disabled = false;
37 submitBtn.textContent = 'Entrar';
38 }
39 });
40 });