var inciar_sesion_python = '/inicio_sesion/login'; function obtenerSiguienteDestinoLogin() { const params = new URLSearchParams(window.location.search); const nextUrl = params.get('next'); if (nextUrl) { return nextUrl; } return window.location.pathname + window.location.search; } function notificarErrorLogin(mensaje) { if (typeof alertaSwitch === 'function') { alertaSwitch('error', 'ERROR', mensaje); } else { alert(mensaje); } } $("#form_iniciar_sesion").on("submit", function(event){ event.preventDefault(); let bandera_error = false; if (!isCorrectEmail('email')) { bandera_error = true; } if (!isCorrectContrasenia('password') || bandera_error) { return; } activarSpinnerOverlayConLabel('Validando cuenta'); let payload = { email: getValueCampo('email'), password: getValueCampo('password') }; if (typeof TokenFormData === 'function') { const csrfToken = TokenFormData().get('csrf_token'); if (csrfToken) { payload.csrf_token = csrfToken; } } $.ajax({ method: "POST", url: inciar_sesion_python, data: payload, timeout: 30000 }).done(function(data){ const data_object = data; if (typeof(data) === typeof('') && data.includes('The CSRF token has expired.')) { notificarErrorLogin('Expiro el tiempo de espera, favor de recargar la pagina'); } else if ('Status' in data_object && data_object.Status == 'OK') { localStorage.setItem('Token', data_object.token); desactivarOverlays(); window.location.assign(obtenerSiguienteDestinoLogin()); return; } else { notificarErrorInJson(data_object); } desactivarOverlays(); }).fail(function(xhr, textStatus){ desactivarOverlays(); if (textStatus === 'timeout') { notificarErrorLogin('El servidor tardo demasiado en responder. Intenta nuevamente.'); return; } if (xhr.status === 400 && xhr.responseJSON && xhr.responseJSON.error) { if (typeof Swal !== 'undefined') { Swal.fire({ icon: 'info', title: 'Página expirada', text: 'Recargando...', timer: 1000, timerProgressBar: true, showConfirmButton: false, allowOutsideClick: false }).then(function() { window.location.reload(); }); } else { window.location.reload(); } return; } if (xhr.responseJSON) { if (xhr.responseJSON.error) { notificarErrorLogin(xhr.responseJSON.error); return; } if (xhr.responseJSON.Message) { notificarErrorLogin(xhr.responseJSON.Message); return; } notificarErrorInJson(xhr.responseJSON); return; } if (xhr.responseText) { try { const data = JSON.parse(xhr.responseText); if (data.error) { notificarErrorLogin(data.error); return; } if (data.Message) { notificarErrorLogin(data.Message); return; } notificarErrorInJson(data); return; } catch (e) {} } notificarErrorLogin(ERROR_CONEXION_INTERRUMPIDA); }); });