| 1 | const EMAIL_INSTITUCIONAL_REGEX = /^[^\s@]+@(tecmilenio\.mx|servicios\.tecmilenio\.mx)$/i; |
| 2 | |
| 3 | document.addEventListener('DOMContentLoaded', async () => { |
| 4 | await Auth.redirectIfAuthenticated(); |
| 5 | |
| 6 | document.getElementById('link-aviso-registro')?.addEventListener('click', () => AvisoPrivacidad.mostrar('Aviso de Privacidad')); |
| 7 | document.getElementById('link-terminos-registro')?.addEventListener('click', () => AvisoPrivacidad.mostrar('Términos y Condiciones')); |
| 8 | |
| 9 | document.getElementById('registro-form').addEventListener('submit', async (e) => { |
| 10 | e.preventDefault(); |
| 11 | Utils.clearAlert('alert-container'); |
| 12 | |
| 13 | const nombre = document.getElementById('nombre').value.trim(); |
| 14 | const apellido = document.getElementById('apellido').value.trim(); |
| 15 | const email = document.getElementById('email').value.trim(); |
| 16 | const contrasena = document.getElementById('contrasena').value; |
| 17 | const confirmarContrasena = document.getElementById('confirmar-contrasena').value; |
| 18 | const identificador = document.getElementById('matricula').value.trim(); |
| 19 | |
| 20 | if (!nombre || !apellido || !email || !contrasena || !confirmarContrasena || !identificador) { |
| 21 | Utils.showAlert('alert-container', 'Completa todos los campos.'); |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | const acepTerminos = document.getElementById('acepta-terminos'); |
| 26 | if (!acepTerminos || !acepTerminos.checked) { |
| 27 | Utils.showAlert('alert-container', 'Debes aceptar el Aviso de Privacidad y los Términos y Condiciones para crear tu cuenta.'); |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | if (!EMAIL_INSTITUCIONAL_REGEX.test(email)) { |
| 32 | Utils.showAlert('alert-container', 'Usa tu correo institucional (@tecmilenio.mx o @servicios.tecmilenio.mx).'); |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | if (contrasena.length < 8 || !/[A-Z]/.test(contrasena) || !/[a-z]/.test(contrasena) || !/\d/.test(contrasena)) { |
| 37 | Utils.showAlert('alert-container', 'La contraseña debe tener al menos 8 caracteres y combinar mayúscula, minúscula y número.'); |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | if (contrasena !== confirmarContrasena) { |
| 42 | Utils.showAlert('alert-container', 'Las contraseñas no coinciden.'); |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | const submitBtn = e.target.querySelector('button[type="submit"]'); |
| 47 | submitBtn.disabled = true; |
| 48 | submitBtn.textContent = 'Creando cuenta...'; |
| 49 | |
| 50 | try { |
| 51 | const payload = { |
| 52 | nombre, |
| 53 | apellido, |
| 54 | email, |
| 55 | contrasena, |
| 56 | rol: 'ESTUDIANTE', |
| 57 | matricula: identificador |
| 58 | }; |
| 59 | |
| 60 | const response = await API.request('/auth/registrar', { |
| 61 | method: 'POST', |
| 62 | body: JSON.stringify(payload) |
| 63 | }); |
| 64 | |
| 65 | Auth.saveSession(response.token, response.usuario); |
| 66 | sessionStorage.setItem('pending-matricula', identificador); |
| 67 | window.location.href = Auth.resolvePath('onboarding.html'); |
| 68 | } catch (error) { |
| 69 | Utils.showAlert('alert-container', error.message || 'No se pudo completar el registro.'); |
| 70 | submitBtn.disabled = false; |
| 71 | submitBtn.textContent = 'Crear cuenta'; |
| 72 | } |
| 73 | }); |
| 74 | }); |