Skip to content

Commit

Permalink
feat: only refresh jwt if valid less than 1 day
Browse files Browse the repository at this point in the history
  • Loading branch information
seriousm4x committed Aug 15, 2023
1 parent 7b5dddf commit b8fdb42
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
22 changes: 22 additions & 0 deletions frontend/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
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;
}
if ($pocketbase.authStore.model?.collectionName === 'users') {
await $pocketbase
.collection('users')
Expand All @@ -76,6 +82,22 @@
});
}
});
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
7 changes: 7 additions & 0 deletions frontend/src/routes/login/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@
import { faLockOpen, faEye } from '@fortawesome/free-solid-svg-icons';
import { toggleVisibility } from '$lib/helpers/forms';
import toast from 'svelte-french-toast';
import { onMount } from 'svelte';
let inputPassword: HTMLInputElement;
let form = {
email: '',
password: ''
};
onMount(() => {
if ($pocketbase.authStore.isValid) {
goto('/');
}
});
function tryAdminThenUser() {
$pocketbase.admins
.authWithPassword(form.email, form.password)
Expand Down

0 comments on commit b8fdb42

Please sign in to comment.