From 8c4c402c2c59bbb72bb01a556b3718bb80b4f6e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxi=20Quo=C3=9F?= Date: Mon, 6 Mar 2023 18:13:25 +0100 Subject: [PATCH] refresh auth tokens to avoid expiration --- frontend/src/routes/+layout.svelte | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/frontend/src/routes/+layout.svelte b/frontend/src/routes/+layout.svelte index 2cd10f50..e7095379 100644 --- a/frontend/src/routes/+layout.svelte +++ b/frontend/src/routes/+layout.svelte @@ -10,7 +10,7 @@ let preferesDark; let isAuth = false; - onMount(() => { + onMount(async () => { // import bootstrap js import('bootstrap/js/dist/dropdown'); import('bootstrap/js/dist/collapse'); @@ -38,9 +38,19 @@ isAuth = state; }); + // load auth from localstorage const pbCookie = localStorage.getItem('pocketbase_auth'); if (pbCookie) { $pocketbase.authStore.loadFromCookie('pb_auth=' + pbCookie); + + // try to refresh auth token if valid + if ($pocketbase.authStore.isValid) { + if ($pocketbase.authStore.model?.collectionName === 'users') { + await $pocketbase.collection('users').authRefresh(); + } else { + await $pocketbase.admins.authRefresh(); + } + } authorizedStore.set($pocketbase.authStore.isValid); } });