enh: exp token check

This commit is contained in:
Timothy Jaeryang Baek committed 2025-04-24 03:12:22 +09:00
1 parent 91e758f3ec
commit 28ec3069de
3 files changed
+103 -85

No files matched your search

+25
View File
@@ -54,6 +54,7 @@
const bc = new BroadcastChannel('active-tab-channel');
let loaded = false;
let tokenTimer = null;
const BREAKPOINT = 768;
@@ -443,6 +444,24 @@
}
};
const checkTokenExpiry = () => {
const exp = $user?.expires_at; // token expiry time in unix timestamp
const now = Math.floor(Date.now() / 1000); // current time in unix timestamp
if (!exp) {
// If no expiry time is set, do nothing
return;
}
if (now >= exp) {
localStorage.removeItem('token');
// redirect to auth page
if ($page.url.pathname !== '/auth') {
goto(`/auth`);
}
}
};
onMount(async () => {
if (typeof window !== 'undefined' && window.applyTheme) {
window.applyTheme();
@@ -560,6 +579,12 @@
await user.set(sessionUser);
await config.set(await getBackendConfig());
// Set up the token expiry check
if (tokenTimer) {
clearInterval(tokenTimer);
}
tokenTimer = setInterval(checkTokenExpiry, 1000);
} else {
// Redirect Invalid Session User to /auth Page
localStorage.removeItem('token');