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 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);
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) {
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(() => {
let continueHash = this.$state.params.continue;

View File

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

View File

@ -36,7 +36,6 @@ export class Layout extends Component {
this.$http.get('AccessTokenConfigs').then(json => {
const firtsResult = json.data[0];
if (!firtsResult) return;
this.renewPeriod = firtsResult.renewPeriod;
this.renewInterval = firtsResult.renewInterval;
const intervalMilliseconds = firtsResult.renewInterval * 1000;
@ -45,18 +44,29 @@ export class Layout extends Component {
}
checkTokenValidity() {
const now = new Date();
const differenceMilliseconds = now - new Date(this.vnToken.created);
const differenceSeconds = Math.floor(differenceMilliseconds / 1000);
console.log(this.vnToken.renewPeriod);
const fecha = new Date(this.vnToken.created); // Fecha representada por un objeto Date
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')
.then(json => {
if (json.data.token) {
let remember = true;
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);
}
});
}