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

@ -54,4 +54,19 @@ function login() {
pin = "";
}
});
}
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 renewPeriod = localStorage.getItem('renewPeriod');
let intervalId, isCheckingToken;
function confirmReset() {
$(".confirm").removeClass('confirmKO');
@ -16,20 +18,50 @@ function printError(msg){
}, 2300);
}
function signIn(user, password) {
function renewToken() {
$.post({
urlPath: 'vnUsers/sign-in',
jsonData: {user, password},
urlPath: 'vnUsers/renewToken',
processData: false,
success: function (data) {
localStorage.setItem("token", data.token);
localStorage.setItem("token", data.id);
localStorage.setItem("ttl", data.ttl);
localStorage.setItem("password", password);
localStorage.setItem("signInTime", Date.now());
localStorage.setItem("created", 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) {
const orgErrorHandler = xhr.error;
const token = localStorage.getItem('token')
@ -53,6 +85,8 @@ $.ajaxPrefilter(function(xhr) {
}
}
if(xhr?.responseJSON.error.code == 'periodNotExceeded') return;
switch (textStatus){
case 'parsererror':
mensaje = 'Requested JSON parse failed';
@ -82,19 +116,9 @@ $.ajaxPrefilter(function(xhr) {
mensaje = 'Ha ocurrido un error, consulta con informática';
}
printError(mensaje);
}
});
});
setInterval(function() {
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
if(renewPeriod) getTokenConfig();