From b7543994898ebbf30c47e28932f34664329adc3c Mon Sep 17 00:00:00 2001 From: kolaente Date: Fri, 12 Jun 2020 20:11:23 +0200 Subject: [PATCH] Redirect the user to login page if the token expired when the tab gets focus again --- src/App.vue | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/App.vue b/src/App.vue index 8ffee64d..afc8cdd9 100644 --- a/src/App.vue +++ b/src/App.vue @@ -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') }