46 lines
1.6 KiB
JavaScript
46 lines
1.6 KiB
JavaScript
import { simpleNotification } from './notify.js'
|
|
|
|
|
|
function validarExpiracion() {
|
|
let timestampActual = Math.floor(Date.now() / 1000); // Timestamp en segundos
|
|
let jsonData = JSON.parse(localStorage.getItem('exp'));
|
|
let expiration = parseInt(jsonData?.exp); // Usamos encadenamiento opcional
|
|
|
|
if (isNaN(expiration)) {
|
|
console.warn("No se encontró un valor válido para 'exp' en localStorage.");
|
|
return;
|
|
}
|
|
|
|
let tiempoRestante = expiration - timestampActual;
|
|
let minutosRestantes = (tiempoRestante / 60).toFixed(0); // 1 decimal
|
|
|
|
let actualizado = false;
|
|
let txt_min = '';
|
|
|
|
if (tiempoRestante < 1020 && tiempoRestante > 780 && jsonData.min15) {
|
|
txt_min = minutosRestantes;
|
|
jsonData.min15 = false;
|
|
actualizado = true;
|
|
} else if (tiempoRestante < 660 && tiempoRestante > 420 && jsonData.min10) {
|
|
txt_min = minutosRestantes;
|
|
jsonData.min10 = false;
|
|
actualizado = true;
|
|
} else if (tiempoRestante < 300 && tiempoRestante > 60 && jsonData.min2) {
|
|
txt_min = minutosRestantes;
|
|
jsonData.min2 = false;
|
|
actualizado = true;
|
|
} else if (tiempoRestante <= 0) {
|
|
window.location.href = "/logout";
|
|
return; // Importante para que no se ejecute el setItem innecesariamente
|
|
}
|
|
|
|
if (actualizado) {
|
|
localStorage.setItem('exp', JSON.stringify(jsonData));
|
|
simpleNotification('⚠️ !Atención¡ ⚠️', `La sesión expirará en ~${txt_min} minutos`, 'warning', 30000)
|
|
}
|
|
|
|
}
|
|
|
|
// Ejecutar la validación cada 5 segundos | 1 seg = 1000 milisegundos | 1 min = 60,000 milisegundos
|
|
setInterval(validarExpiracion, 5000); //180,000 = 3 minutos
|