refs 6274 calling renewToken
gitea/worker-time-control/pipeline/head This commit looks good Details

This commit is contained in:
Jorge Penadés 2023-12-05 15:56:51 +01:00
parent 7b9d0c5310
commit 7c5386058b
2 changed files with 57 additions and 18 deletions

View File

@ -55,3 +55,18 @@ function login() {
} }
}); });
} }
function signIn(user, password) {
$.post({
urlPath: 'vnUsers/sign-in',
jsonData: {user, password},
processData: false,
success: function (data) {
localStorage.setItem("token", data.token);
localStorage.setItem("ttl", data.ttl);
localStorage.setItem("password", password);
localStorage.setItem("created", Date.now());
getTokenConfig();
},
})
}

View File

@ -1,4 +1,6 @@
const user = "timeControl"; const user = "timeControl";
const renewPeriod = localStorage.getItem('renewPeriod');
let intervalId, isCheckingToken;
function confirmReset() { function confirmReset() {
$(".confirm").removeClass('confirmKO'); $(".confirm").removeClass('confirmKO');
@ -16,20 +18,50 @@ function printError(msg){
}, 2300); }, 2300);
} }
function signIn(user, password) { function renewToken() {
$.post({ $.post({
urlPath: 'vnUsers/sign-in', urlPath: 'vnUsers/renewToken',
jsonData: {user, password},
processData: false, processData: false,
success: function (data) { success: function (data) {
localStorage.setItem("token", data.token); localStorage.setItem("token", data.id);
localStorage.setItem("ttl", data.ttl); localStorage.setItem("ttl", data.ttl);
localStorage.setItem("password", password); localStorage.setItem("created", Date.now());
localStorage.setItem("signInTime", Date.now());
}, },
}) })
} }
function getTokenConfig() {
const filter = {fields: ['renewInterval', 'renewPeriod']};
$.get({
urlPath: 'AccessTokenConfigs/findOne',
jsonData: filter,
processData: false,
success: function (data) {
if (!data) return;
localStorage.setItem('renewPeriod', data.renewPeriod);
clearInterval(intervalId);
intervalId = setInterval(() => checkValidity(), data.renewInterval * 10);
},
})
}
function checkValidity() {
const created = localStorage.getItem('created');
const ttl = localStorage.getItem('ttl');
if (isCheckingToken || !created) return;
isCheckingToken = true;
const renewPeriodInSeconds = Math.min(ttl, renewPeriod) * 1000;
const maxDate = created + renewPeriodInSeconds;
const now = new Date();
if (now.getTime() <= maxDate) return isCheckingToken = false;
renewToken();
isCheckingToken = false;
}
$.ajaxPrefilter(function(xhr) { $.ajaxPrefilter(function(xhr) {
const orgErrorHandler = xhr.error; const orgErrorHandler = xhr.error;
const token = localStorage.getItem('token') const token = localStorage.getItem('token')
@ -53,6 +85,8 @@ $.ajaxPrefilter(function(xhr) {
} }
} }
if(xhr?.responseJSON.error.code == 'periodNotExceeded') return;
switch (textStatus){ switch (textStatus){
case 'parsererror': case 'parsererror':
mensaje = 'Requested JSON parse failed'; mensaje = 'Requested JSON parse failed';
@ -82,19 +116,9 @@ $.ajaxPrefilter(function(xhr) {
mensaje = 'Ha ocurrido un error, consulta con informática'; mensaje = 'Ha ocurrido un error, consulta con informática';
} }
printError(mensaje); printError(mensaje);
} }
}); });
}); });
setInterval(function() { if(renewPeriod) getTokenConfig();
const ttl = localStorage.getItem('ttl');
const password = localStorage.getItem('password');
if(ttl && password) {
const now = Date.now();
const signInTime = localStorage.getItem('signInTime');
const countdown = now - signInTime;
if(countdown < ttl) signIn(user, password)
}
},3600000)// 1 hour