From ed4d41e2d888b117b68b36e8a9a5765a4f67d5d2 Mon Sep 17 00:00:00 2001 From: konrad Date: Thu, 19 Dec 2019 20:50:07 +0000 Subject: [PATCH] Add automatic user token renew (#43) --- src/App.vue | 5 +++++ src/auth/index.js | 19 +++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/App.vue b/src/App.vue index e267fec3a..cf7bf96b1 100644 --- a/src/App.vue +++ b/src/App.vue @@ -248,6 +248,11 @@ window.location.reload(); } ); + + // Schedule a token renew every 60 minutes + setTimeout(() => { + auth.renewToken() + }, 1000 * 60 * 60) }, watch: { // call the method again if the route changes diff --git a/src/auth/index.js b/src/auth/index.js index 876ef6bc5..1f6d9904c 100644 --- a/src/auth/index.js +++ b/src/auth/index.js @@ -76,16 +76,31 @@ export default { }, linkShareAuth(hash) { - return HTTP.post('/shares/'+hash+'/auth') + return HTTP.post('/shares/' + hash + '/auth') .then(r => { localStorage.setItem('token', r.data.token) this.getUserInfos() return Promise.resolve(r.data) - }).catch(e => { + }).catch(e => { return Promise.reject(e) }) }, + renewToken() { + HTTP.post('user/token', null, { + headers: { + Authorization: 'Bearer ' + localStorage.getItem('token'), + } + }) + .then(r => { + localStorage.setItem('token', r.data.token) + }) + .catch(e => { + // eslint-disable-next-line + console.log('Error renewing token: ', e) + }) + }, + checkAuth() { let jwt = localStorage.getItem('token') this.getUserInfos()