fix: muestra mensaje de error
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Vicent Llopis 2023-06-27 15:34:35 +02:00
parent 7d2076ff90
commit 6b61d4cca1
5 changed files with 30 additions and 10 deletions

View File

@ -34,6 +34,10 @@ module.exports = Self => {
const user = await Self.findById(userId); const user = await Self.findById(userId);
const accessToken = await user.createAccessToken(); const accessToken = await user.createAccessToken();
return {token: accessToken.id, created: accessToken.created}; const renewPeriod = accessToken.ttl > accessTokenConfig.renewPeriod
? accessToken.ttl
: accessTokenConfig.renewPeriod;
return {token: accessToken.id, renewPeriod};
}; };
}; };

View File

@ -76,6 +76,6 @@ module.exports = Self => {
let loginInfo = Object.assign({password}, userInfo); let loginInfo = Object.assign({password}, userInfo);
token = await Self.login(loginInfo, 'user'); token = await Self.login(loginInfo, 'user');
return {token: token.id, created: token.created}; return {token: token.id, created: token.created, ttl: token.ttl};
}; };
}; };

View File

@ -64,7 +64,7 @@ export default class Auth {
} }
onLoginOk(json, remember) { onLoginOk(json, remember) {
this.vnToken.set(json.data.token, json.data.created, remember); this.vnToken.set(json.data.token, json.data.created, json.data.ttl, remember);
return this.loadAcls().then(() => { return this.loadAcls().then(() => {
let continueHash = this.$state.params.continue; let continueHash = this.$state.params.continue;

View File

@ -10,32 +10,38 @@ export default class Token {
try { try {
this.token = sessionStorage.getItem('vnToken'); this.token = sessionStorage.getItem('vnToken');
this.created = sessionStorage.getItem('vnTokenCreated'); this.created = sessionStorage.getItem('vnTokenCreated');
this.renewPeriod = sessionStorage.getItem('vnTokenRenewPeriod');
if (!this.token) { if (!this.token) {
this.token = localStorage.getItem('vnToken'); this.token = localStorage.getItem('vnToken');
this.created = localStorage.getItem('vnTokenCreated'); this.created = localStorage.getItem('vnTokenCreated');
this.renewPeriod = localStorage.getItem('vnTokenRenewPeriod');
} }
} catch (e) {} } catch (e) {}
} }
set(token, created, remember) { set(token, created, renewPeriod, remember) {
this.unset(); this.unset();
try { try {
if (remember) { if (remember) {
localStorage.setItem('vnToken', token); localStorage.setItem('vnToken', token);
localStorage.setItem('vnTokenCreated', created); localStorage.setItem('vnTokenCreated', created);
localStorage.setItem('vnTokenRenewPeriod', renewPeriod);
} else { } else {
sessionStorage.setItem('vnToken', token); sessionStorage.setItem('vnToken', token);
sessionStorage.setItem('vnTokenCreated', created); sessionStorage.setItem('vnTokenCreated', created);
sessionStorage.setItem('vnTokenRenewPeriod', renewPeriod);
} }
} catch (e) {} } catch (e) {}
this.token = token; this.token = token;
this.created = created; this.created = created;
this.renewPeriod = renewPeriod;
} }
unset() { unset() {
localStorage.removeItem('vnToken'); localStorage.removeItem('vnToken');
sessionStorage.removeItem('vnToken'); sessionStorage.removeItem('vnToken');
this.token = null; this.token = null;
this.created = null; this.created = null;
this.renewPeriod = null;
} }
} }

View File

@ -36,7 +36,6 @@ export class Layout extends Component {
this.$http.get('AccessTokenConfigs').then(json => { this.$http.get('AccessTokenConfigs').then(json => {
const firtsResult = json.data[0]; const firtsResult = json.data[0];
if (!firtsResult) return; if (!firtsResult) return;
this.renewPeriod = firtsResult.renewPeriod;
this.renewInterval = firtsResult.renewInterval; this.renewInterval = firtsResult.renewInterval;
const intervalMilliseconds = firtsResult.renewInterval * 1000; const intervalMilliseconds = firtsResult.renewInterval * 1000;
@ -45,18 +44,29 @@ export class Layout extends Component {
} }
checkTokenValidity() { checkTokenValidity() {
const now = new Date(); console.log(this.vnToken.renewPeriod);
const differenceMilliseconds = now - new Date(this.vnToken.created); const fecha = new Date(this.vnToken.created); // Fecha representada por un objeto Date
const differenceSeconds = Math.floor(differenceMilliseconds / 1000); const duracionEnSegundos = this.vnToken.renewPeriod; // Duración en segundos (número)
const fechaEnMilisegundos = fecha.getTime(); // Convertir fecha a milisegundos
const duracionEnMilisegundos = duracionEnSegundos * 1000; // Convertir duración a milisegundos
const nuevaFechaEnMilisegundos = fechaEnMilisegundos + duracionEnMilisegundos; // Sumar los milisegundos
const nuevaFecha = new Date(nuevaFechaEnMilisegundos); // Crear una nueva fecha a partir de los milisegundos
if (differenceSeconds > this.renewPeriod) { // const created = new Date(this.vnToken.created);
// const createdInSeconds = Math.floor(created.getTime() / 1000);
// const maxSeconds = createdInSeconds + this.vnToken.ttl;
// const maxDate = new Date(maxSeconds);
const now = new Date();
console.log(now > nuevaFecha, now, nuevaFecha);
if (now > nuevaFecha) {
this.$http.post('VnUsers/renewToken') this.$http.post('VnUsers/renewToken')
.then(json => { .then(json => {
if (json.data.token) { if (json.data.token) {
let remember = true; let remember = true;
if (window.sessionStorage.vnToken) remember = false; if (window.sessionStorage.vnToken) remember = false;
this.vnToken.set(json.data.token, json.data.created, remember); this.vnToken.set(json.data.token, new Date(), json.data.renewPeriod, remember);
} }
}); });
} }