Add automatic user token renew (#43)
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
konrad 2019-12-19 20:50:07 +00:00
parent 6b7fe8ee47
commit ed4d41e2d8
2 changed files with 22 additions and 2 deletions

View File

@ -248,6 +248,11 @@
window.location.reload(); window.location.reload();
} }
); );
// Schedule a token renew every 60 minutes
setTimeout(() => {
auth.renewToken()
}, 1000 * 60 * 60)
}, },
watch: { watch: {
// call the method again if the route changes // call the method again if the route changes

View File

@ -76,16 +76,31 @@ export default {
}, },
linkShareAuth(hash) { linkShareAuth(hash) {
return HTTP.post('/shares/'+hash+'/auth') return HTTP.post('/shares/' + hash + '/auth')
.then(r => { .then(r => {
localStorage.setItem('token', r.data.token) localStorage.setItem('token', r.data.token)
this.getUserInfos() this.getUserInfos()
return Promise.resolve(r.data) return Promise.resolve(r.data)
}).catch(e => { }).catch(e => {
return Promise.reject(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() { checkAuth() {
let jwt = localStorage.getItem('token') let jwt = localStorage.getItem('token')
this.getUserInfos() this.getUserInfos()