130 lines
4.1 KiB
JavaScript
130 lines
4.1 KiB
JavaScript
const renewPeriod = localStorage.getItem('renewPeriod');
|
|
let intervalId, isCheckingToken;
|
|
|
|
function confirmReset() {
|
|
$(".confirm").removeClass('confirmKO');
|
|
$(".txtConfirm").empty();
|
|
}
|
|
|
|
function printError(msg){
|
|
confirmReset();
|
|
$(".txtConfirm").append(msg);
|
|
$(".confirm").addClass('confirmKO');
|
|
$(".confirm").fadeIn(200);
|
|
setTimeout(function() {
|
|
$(".confirm").fadeOut(200);
|
|
setTimeout(confirmReset, 200);
|
|
}, 2300);
|
|
}
|
|
|
|
function renewToken() {
|
|
$.post({
|
|
urlPath: 'vnUsers/renewToken',
|
|
processData: false,
|
|
success: function (data) {
|
|
localStorage.setItem("token", data.id);
|
|
localStorage.setItem("ttl", data.ttl);
|
|
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 * 1000);
|
|
},
|
|
})
|
|
}
|
|
|
|
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')
|
|
|
|
Object.assign(xhr, {
|
|
url: `/api/${xhr.urlPath}`,
|
|
headers: {
|
|
Authorization : token
|
|
},
|
|
timeout: 2000,
|
|
contentType: 'application/json; charset=utf-8',
|
|
dataType: 'json',
|
|
processData: false,
|
|
data: JSON.stringify(xhr.jsonData),
|
|
error: function(xhr, textStatus, err) {
|
|
if (orgErrorHandler) {
|
|
try {
|
|
orgErrorHandler(xhr, textStatus, err);
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
}
|
|
|
|
if(xhr?.responseJSON?.error?.code == 'periodNotExceeded') return;
|
|
|
|
switch (textStatus){
|
|
case 'parsererror':
|
|
mensaje = 'Requested JSON parse failed';
|
|
break;
|
|
case 'timeout':
|
|
mensaje = 'Time out error';
|
|
break;
|
|
case 'abort':
|
|
mensaje = 'Ajax request aborted';
|
|
break;
|
|
case 'error':
|
|
if (xhr?.responseJSON?.error?.name == 'UserError') {
|
|
mensaje = xhr.responseJSON.error.message;
|
|
break;
|
|
}
|
|
switch (xhr.status){
|
|
case 0:
|
|
mensaje = 'Not connect: Verify Network';
|
|
break;
|
|
case 504:
|
|
mensaje = 'No se ha podido conectar con Salix, consulta con informática';
|
|
break;
|
|
case 555:
|
|
mensaje = JSON.parse(xhr.statusText).Message;
|
|
break;
|
|
default:
|
|
if (xhr.status >= 400 && xhr.status < 500)
|
|
mensaje = xhr.statusText;
|
|
else
|
|
mensaje = 'Ha ocurrido un error, consulta con informática';
|
|
}
|
|
break;
|
|
default:
|
|
mensaje = 'Ha ocurrido un error, consulta con informática';
|
|
}
|
|
printError(mensaje);
|
|
|
|
}
|
|
});
|
|
});
|
|
|
|
if(renewPeriod) getTokenConfig(); |