Cambio de contraseña desde dashboard de cualquier tipo de usuario
This commit is contained in:
@@ -173,16 +173,16 @@ if (!isset($_SESSION['email_recuperacion'])) {
|
||||
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
const form = document.getElementById('formCodigo');
|
||||
const mensaje = document.getElementById('mensaje');
|
||||
const inputCodigo = document.getElementById('codigo');
|
||||
const form = document.getElementById('formCodigo');
|
||||
const mensaje = document.getElementById('mensaje');
|
||||
const inputCodigo = document.getElementById('codigo');
|
||||
const btnVerificar = document.getElementById('btnVerificar');
|
||||
const btnReenviar = document.getElementById('btnReenviarRespaldo');
|
||||
const clearBtn = document.getElementById('clearCode');
|
||||
const btnReenviar = document.getElementById('btnReenviarRespaldo');
|
||||
const clearBtn = document.getElementById('clearCode');
|
||||
const intentosInfo = document.getElementById('intentosInfo');
|
||||
|
||||
let intentosRealizados = 0;
|
||||
const maxIntentos = 5;
|
||||
const maxIntentos = 3; // ⬅️ Cambiado de 5 a 3 para el controlador interno
|
||||
|
||||
// Auto-focus en el input al cargar
|
||||
inputCodigo.focus();
|
||||
@@ -220,14 +220,6 @@ if (!isset($_SESSION['email_recuperacion'])) {
|
||||
}
|
||||
});
|
||||
|
||||
// Auto-envío cuando se completen 6 dígitos (opcional)
|
||||
inputCodigo.addEventListener('input', function() {
|
||||
if (this.value.length === 6) {
|
||||
// Opcional: enviar automáticamente después de un breve delay
|
||||
// setTimeout(() => form.dispatchEvent(new Event('submit')), 500);
|
||||
}
|
||||
});
|
||||
|
||||
// Manejo del formulario
|
||||
form.addEventListener('submit', function(e) {
|
||||
e.preventDefault();
|
||||
@@ -235,7 +227,7 @@ if (!isset($_SESSION['email_recuperacion'])) {
|
||||
const codigo = inputCodigo.value.trim();
|
||||
if (!validarCodigo(codigo)) return;
|
||||
|
||||
enviarCodigo(codigo);
|
||||
enviarCodigoInterno(codigo);
|
||||
});
|
||||
|
||||
// Validación del código
|
||||
@@ -260,8 +252,8 @@ if (!isset($_SESSION['email_recuperacion'])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Enviar código para verificación
|
||||
function enviarCodigo(codigo) {
|
||||
// ⬅️ NUEVA FUNCIÓN: Enviar código para verificación INTERNA
|
||||
function enviarCodigoInterno(codigo) {
|
||||
// Mostrar estado de carga
|
||||
btnVerificar.disabled = true;
|
||||
btnVerificar.classList.add('loading');
|
||||
@@ -270,34 +262,43 @@ if (!isset($_SESSION['email_recuperacion'])) {
|
||||
const formData = new FormData();
|
||||
formData.append('codigo', codigo);
|
||||
|
||||
fetch('/IMPORTADORES/login/verificarCodigo', {
|
||||
fetch('/IMPORTADORES/reset/verificarCodigoInterno', {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
credentials: 'same-origin'
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.blocked) {
|
||||
mostrarError(data.message); // <-- función personalizada con un div bonito
|
||||
setTimeout(() => {
|
||||
window.location.href = data.redirect;
|
||||
}, 4000);
|
||||
.then(response => {
|
||||
// ⬅️ IMPORTANTE: Verificar si la respuesta es un redirect (302/200 HTML)
|
||||
if (response.ok && response.headers.get('content-type')?.includes('text/html')) {
|
||||
// Si el servidor devolvió HTML, significa que hubo un redirect exitoso
|
||||
mostrarExito("✅ Código verificado correctamente. Redirigiendo...");
|
||||
inputCodigo.classList.add('success');
|
||||
inputCodigo.disabled = true;
|
||||
|
||||
setTimeout(() => {
|
||||
window.location.href = '/IMPORTADORES/reset/cambiarPasswordInternoView';
|
||||
}, 1500);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Si no es HTML, intentar parsear como JSON (para errores)
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
if (!data) return; // Ya fue manejado arriba como éxito
|
||||
|
||||
// Manejar respuestas de error JSON
|
||||
if (data.success) {
|
||||
// Código correcto
|
||||
mostrarExito(data.message);
|
||||
inputCodigo.classList.add('success');
|
||||
inputCodigo.disabled = true;
|
||||
|
||||
// Redirigir al formulario de cambio de contraseña
|
||||
setTimeout(() => {
|
||||
window.location.href = '/IMPORTADORES/login/cambiarPasswordVista';
|
||||
window.location.href = '/IMPORTADORES/reset/cambiarPasswordInternoView';
|
||||
}, 1500);
|
||||
|
||||
} else {
|
||||
// Código incorrecto
|
||||
manejarCodigoIncorrecto(data);
|
||||
manejarCodigoIncorrectoInterno(data);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
@@ -314,57 +315,27 @@ if (!isset($_SESSION['email_recuperacion'])) {
|
||||
});
|
||||
}
|
||||
|
||||
// Mostrar error de cuenta bloqueada
|
||||
function mostrarError(mensaje) {
|
||||
const mensajeDiv = document.getElementById('mensaje');
|
||||
mensajeDiv.innerHTML = `
|
||||
<div class="text-danger fade-in">
|
||||
<i class="fas fa-exclamation-triangle me-2"></i> ${mensaje}
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
// Manejar código incorrecto
|
||||
function manejarCodigoIncorrecto(data) {
|
||||
// ⬅️ NUEVA FUNCIÓN: Manejar errores específicos del controlador interno
|
||||
function manejarCodigoIncorrectoInterno(data) {
|
||||
intentosRealizados++;
|
||||
|
||||
mostrarError(data.message);
|
||||
mostrarError(data.message || "❌ Código incorrecto.");
|
||||
inputCodigo.classList.add('error');
|
||||
|
||||
// Limpiar input y enfocar para nuevo intento
|
||||
limpiarYEnfocarInput();
|
||||
|
||||
// Actualizar contador de intentos
|
||||
actualizarContadorIntentos();
|
||||
actualizarContadorIntentosInterno();
|
||||
|
||||
// Si se bloqueó el código
|
||||
if (data.blocked) {
|
||||
bloquearFormulario();
|
||||
// Si se excedieron los intentos (3 para interno)
|
||||
if (intentosRealizados >= maxIntentos) {
|
||||
bloquearFormularioInterno();
|
||||
}
|
||||
}
|
||||
|
||||
// Limpiar input y enfocar
|
||||
function limpiarYEnfocarInput() {
|
||||
setTimeout(() => {
|
||||
inputCodigo.value = '';
|
||||
inputCodigo.classList.remove('error');
|
||||
inputCodigo.focus();
|
||||
clearBtn.style.display = 'none';
|
||||
}, 1500); // Esperar 1.5 segundos antes de limpiar
|
||||
}
|
||||
|
||||
// Actualizar contador visual
|
||||
function actualizarContadorVisual() {
|
||||
const longitud = inputCodigo.value.length;
|
||||
if (longitud > 0) {
|
||||
inputCodigo.setAttribute('placeholder', '0'.repeat(6 - longitud) + inputCodigo.value);
|
||||
} else {
|
||||
inputCodigo.setAttribute('placeholder', '000000');
|
||||
}
|
||||
}
|
||||
|
||||
// Actualizar contador de intentos
|
||||
function actualizarContadorIntentos() {
|
||||
// ⬅️ MODIFICADA: Actualizar contador de intentos para interno
|
||||
function actualizarContadorIntentosInterno() {
|
||||
if (intentosRealizados > 0) {
|
||||
const restantes = maxIntentos - intentosRealizados;
|
||||
intentosInfo.innerHTML = `
|
||||
@@ -375,21 +346,43 @@ if (!isset($_SESSION['email_recuperacion'])) {
|
||||
}
|
||||
}
|
||||
|
||||
// Bloquear formulario cuando se exceden intentos
|
||||
function bloquearFormulario() {
|
||||
// ⬅️ MODIFICADA: Bloquear formulario para interno
|
||||
function bloquearFormularioInterno() {
|
||||
inputCodigo.disabled = true;
|
||||
btnVerificar.disabled = true;
|
||||
btnReenviar.disabled = true;
|
||||
if (btnReenviar) btnReenviar.disabled = true;
|
||||
|
||||
intentosInfo.innerHTML = `
|
||||
<i class="fas fa-ban text-danger me-1"></i>
|
||||
<span class="text-danger">Código bloqueado por seguridad</span>
|
||||
<span class="text-danger">Demasiados intentos fallidos</span>
|
||||
`;
|
||||
|
||||
// Redirigir a solicitar nuevo código después de 5 segundos
|
||||
mostrarError("❌ Demasiados intentos fallidos. Redirigiendo...");
|
||||
|
||||
// Redirigir a la página de seguridad después de 3 segundos
|
||||
setTimeout(() => {
|
||||
window.location.href = '/IMPORTADORES/login/recuperar';
|
||||
}, 5000);
|
||||
window.location.href = '/IMPORTADORES/seguridad/index';
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
// Limpiar input y enfocar
|
||||
function limpiarYEnfocarInput() {
|
||||
setTimeout(() => {
|
||||
inputCodigo.value = '';
|
||||
inputCodigo.classList.remove('error');
|
||||
inputCodigo.focus();
|
||||
clearBtn.style.display = 'none';
|
||||
}, 1500);
|
||||
}
|
||||
|
||||
// Actualizar contador visual
|
||||
function actualizarContadorVisual() {
|
||||
const longitud = inputCodigo.value.length;
|
||||
if (longitud > 0) {
|
||||
inputCodigo.setAttribute('placeholder', '0'.repeat(6 - longitud) + inputCodigo.value);
|
||||
} else {
|
||||
inputCodigo.setAttribute('placeholder', '000000');
|
||||
}
|
||||
}
|
||||
|
||||
// Funciones de utilidad para mensajes
|
||||
@@ -414,40 +407,39 @@ if (!isset($_SESSION['email_recuperacion'])) {
|
||||
mensaje.innerHTML = '';
|
||||
}
|
||||
|
||||
// Manejo del botón de reenvío
|
||||
btnReenviar.addEventListener("click", function() {
|
||||
const btnOriginalText = this.innerHTML;
|
||||
|
||||
// Deshabilitar botón temporalmente
|
||||
this.disabled = true;
|
||||
this.innerHTML = '<i class="fas fa-spinner fa-spin me-1"></i>Enviando...';
|
||||
|
||||
fetch("/IMPORTADORES/login/reenviarCodigo", {
|
||||
method: "POST",
|
||||
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
|
||||
body: "accion=reenviarCodigo"
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
mostrarInfo(data.message);
|
||||
// Manejo del botón de reenvío (si existe)
|
||||
if (btnReenviar) {
|
||||
btnReenviar.addEventListener("click", function() {
|
||||
const btnOriginalText = this.innerHTML;
|
||||
|
||||
// Habilitar botón después de 30 segundos
|
||||
setTimeout(() => {
|
||||
this.disabled = true;
|
||||
this.innerHTML = '<i class="fas fa-spinner fa-spin me-1"></i>Enviando...';
|
||||
|
||||
fetch("/IMPORTADORES/reset/reenviarCodigoInterno", {
|
||||
method: "POST",
|
||||
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
|
||||
body: "accion=reenviarCodigo"
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
mostrarInfo(data.message);
|
||||
|
||||
setTimeout(() => {
|
||||
this.disabled = false;
|
||||
this.innerHTML = btnOriginalText;
|
||||
}, 30000);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
mostrarError("❌ Error al reenviar código.");
|
||||
|
||||
this.disabled = false;
|
||||
this.innerHTML = btnOriginalText;
|
||||
}, 30000);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
mostrarError("❌ Error al reenviar código.");
|
||||
|
||||
// Restaurar botón en caso de error
|
||||
this.disabled = false;
|
||||
this.innerHTML = btnOriginalText;
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Manejar tecla Enter en cualquier parte del formulario
|
||||
// Manejar tecla Enter
|
||||
document.addEventListener('keypress', function(e) {
|
||||
if (e.key === 'Enter' && !btnVerificar.disabled) {
|
||||
form.dispatchEvent(new Event('submit'));
|
||||
|
||||
Reference in New Issue
Block a user