| 1 | const EMAIL_INSTITUCIONAL_REGEX = /^[^\s@]+@(tecmilenio\.mx|servicios\.tecmilenio\.mx)$/i; |
| 2 | |
| 3 | document.addEventListener('DOMContentLoaded', () => { |
| 4 | Auth.redirectIfAuthenticated(); |
| 5 | |
| 6 | document.getElementById('registro-form').addEventListener('submit', async (e) => { |
| 7 | e.preventDefault(); |
| 8 | Utils.clearAlert('alert-container'); |
| 9 | |
| 10 | const nombre = document.getElementById('nombre').value.trim(); |
| 11 | const apellido = document.getElementById('apellido').value.trim(); |
| 12 | const email = document.getElementById('email').value.trim(); |
| 13 | const contrasena = document.getElementById('contrasena').value; |
| 14 | const confirmarContrasena = document.getElementById('confirmar-contrasena').value; |
| 15 | |
| 16 | if (!nombre || !apellido || !email || !contrasena || !confirmarContrasena) { |
| 17 | Utils.showAlert('alert-container', 'Completa todos los campos.'); |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | if (!EMAIL_INSTITUCIONAL_REGEX.test(email)) { |
| 22 | Utils.showAlert('alert-container', 'Usa tu correo institucional (@tecmilenio.mx o @servicios.tecmilenio.mx).'); |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | if (contrasena.length < 6) { |
| 27 | Utils.showAlert('alert-container', 'La contraseña debe tener al menos 6 caracteres.'); |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | if (contrasena !== confirmarContrasena) { |
| 32 | Utils.showAlert('alert-container', 'Las contraseñas no coinciden.'); |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | const submitBtn = e.target.querySelector('button[type="submit"]'); |
| 37 | submitBtn.disabled = true; |
| 38 | submitBtn.textContent = 'Creando cuenta...'; |
| 39 | |
| 40 | try { |
| 41 | const response = await API.request('/auth/registrar', { |
| 42 | method: 'POST', |
| 43 | body: JSON.stringify({ nombre, apellido, email, contrasena }) |
| 44 | }); |
| 45 | |
| 46 | Auth.saveSession(response.data.token, response.data.usuario); |
| 47 | window.location.href = Auth.resolvePath('pages/inicio.html'); |
| 48 | } catch (error) { |
| 49 | Utils.showAlert('alert-container', error.message || 'No se pudo completar el registro.'); |
| 50 | submitBtn.disabled = false; |
| 51 | submitBtn.textContent = 'Crear cuenta'; |
| 52 | } |
| 53 | }); |
| 54 | }); |