Redirect the user to login page if the token expired when the tab gets focus again

This commit is contained in:
kolaente 2020-06-12 20:11:23 +02:00
parent dc29604f94
commit b754399489
No known key found for this signature in database
GPG Key ID: F40E70337AB24C9B
1 changed files with 16 additions and 1 deletions

View File

@ -354,8 +354,23 @@
// Check if the token is still valid if the window gets focus again to maybe renew it
window.addEventListener('focus', () => {
if(!this.userAuthenticated) {
return
}
const expiresIn = this.userInfo.exp - +new Date() / 1000
// If the token expiry is negative, it is already expired and we have no choice but to redirect
// the user to the login page
if (expiresIn < 0) {
this.$store.dispatch('auth/checkAuth')
router.push({name: 'login'})
return
}
// Check if the token is valid for less than 60 hours and renew if thats the case
if (this.userInfo.exp - +new Date() / 1000 < 60 * 3600) {
if (expiresIn < 60 * 3600) {
this.$store.dispatch('auth/renewToken')
console.log('renewed token')
}