Skip to content

Commit

Permalink
fix: remove date check for auth token, always refresh #481
Browse files Browse the repository at this point in the history
  • Loading branch information
seriousm4x committed Mar 25, 2024
1 parent 6759861 commit 094a709
Showing 1 changed file with 4 additions and 35 deletions.
39 changes: 4 additions & 35 deletions frontend/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -58,58 +58,27 @@
// 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 {
await $pocketbase
.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);
}
</script>

<svelte:head>
Expand Down

0 comments on commit 094a709

Please sign in to comment.