From cbe84d71390a8c2ff85ebc07010f35f3a59110ec Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Fri, 23 May 2025 01:08:28 +0400 Subject: [PATCH] enh: token expiry buffer --- src/routes/+layout.svelte | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 96936c338..5d69f559a 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -7,7 +7,7 @@ stiffness: 0.05 }); - import { onMount, tick, setContext } from 'svelte'; + import { onMount, tick, setContext, onDestroy } from 'svelte'; import { config, user, @@ -454,6 +454,7 @@ } }; + const TOKEN_EXPIRY_BUFFER = 60; // seconds const checkTokenExpiry = async () => { const exp = $user?.expires_at; // token expiry time in unix timestamp const now = Math.floor(Date.now() / 1000); // current time in unix timestamp @@ -463,7 +464,7 @@ return; } - if (now >= exp) { + if (now >= exp - TOKEN_EXPIRY_BUFFER) { const res = await userSignOut(); user.set(null); localStorage.removeItem('token'); @@ -508,6 +509,9 @@ if (document.visibilityState === 'visible') { isLastActiveTab.set(true); // This tab is now the active tab bc.postMessage('active'); // Notify other tabs that this tab is active + + // Check token expiry when the tab becomes active + checkTokenExpiry(); } };