From 79823605f65c0974e0803ee7207c1270b97eabe4 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 29 Jun 2023 12:15:32 +0200 Subject: [PATCH] refs #5554 fix: no te tira al hacer F5 --- front/core/services/interceptor.js | 3 ++- front/core/services/token.js | 12 ++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/front/core/services/interceptor.js b/front/core/services/interceptor.js index 1c7b1a460..0c3253c69 100644 --- a/front/core/services/interceptor.js +++ b/front/core/services/interceptor.js @@ -4,7 +4,8 @@ import HttpError from 'core/lib/http-error'; interceptor.$inject = ['$q', 'vnApp', '$translate']; function interceptor($q, vnApp, $translate) { let apiPath = 'api/'; - let token; + let token = sessionStorage.getItem('vnToken') + ?? localStorage.getItem('vnToken'); return { setToken(newToken) { diff --git a/front/core/services/token.js b/front/core/services/token.js index d0e0b7ced..8f9f80e5c 100644 --- a/front/core/services/token.js +++ b/front/core/services/token.js @@ -40,7 +40,9 @@ export default class Token { this.setStorage(localStorage, token, created, ttl); else this.setStorage(sessionStorage, token, created, ttl); - } catch (e) {} + } catch (err) { + console.error(err); + } } unset() { @@ -56,13 +58,15 @@ export default class Token { getStorage(storage) { this.token = storage.getItem('vnToken'); - this.created = storage.getItem('vnTokenCreated'); + if (!this.token) return; + const created = storage.getItem('vnTokenCreated'); + this.created = created && new Date(created); this.renewPeriod = storage.getItem('vnTokenRenewPeriod'); } setStorage(storage, token, created, ttl) { storage.setItem('vnToken', token); - storage.setItem('vnTokenCreated', created); + storage.setItem('vnTokenCreated', created.toJSON()); storage.setItem('vnTokenTtl', ttl); } @@ -85,7 +89,7 @@ export default class Token { } checkValidity() { - if (this.checking) return; + if (this.checking || !this.created) return; this.checking = true; const renewPeriod = Math.min(this.ttl, this.renewPeriod) * 1000; const maxDate = this.created.getTime() + renewPeriod;