From 094a7092b0e06facfd157558f16596961466dbba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxi=20Quo=C3=9F?= Date: Mon, 25 Mar 2024 11:19:02 +0100 Subject: [PATCH] fix: remove date check for auth token, always refresh #481 --- frontend/src/routes/+layout.svelte | 39 +++--------------------------- 1 file changed, 4 insertions(+), 35 deletions(-) diff --git a/frontend/src/routes/+layout.svelte b/frontend/src/routes/+layout.svelte index dc7dd14c..23aa5070 100644 --- a/frontend/src/routes/+layout.svelte +++ b/frontend/src/routes/+layout.svelte @@ -58,31 +58,15 @@ // redirect to welcome page if setup is not completed if ($settingsPub.setup_completed === false && $page.url.pathname !== '/welcome') { + $pocketbase.authStore.clear(); goto('/welcome'); return; } - // load auth from localstorage - const pbCookie = localStorage.getItem('pocketbase_auth'); - if (!pbCookie) { - goto('/login'); - return; - } - - $pocketbase.authStore.loadFromCookie('pb_auth=' + pbCookie); - if (!$pocketbase.authStore.isValid) { - goto('/login'); - return; - } - - // only refresh token if valid less than 1 day - const jwt = parseJwt($pocketbase.authStore.token); - if (jwt.exp > Date.now() / 1000 + 60 * 60 * 24) { - return; - } - + // refresh auth token if ($pocketbase.authStore.isAdmin) { await $pocketbase.admins.authRefresh().catch(() => { + $pocketbase.authStore.clear(); goto('/login'); }); } else { @@ -90,26 +74,11 @@ .collection('users') .authRefresh() .catch(() => { + $pocketbase.authStore.clear(); goto('/login'); }); } }); - - function parseJwt(token: string) { - var base64Url = token.split('.')[1]; - var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/'); - var jsonPayload = decodeURIComponent( - window - .atob(base64) - .split('') - .map(function (c) { - return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); - }) - .join('') - ); - - return JSON.parse(jsonPayload); - }