feat: only refresh jwt if valid less than 1 day

This commit is contained in:
seriousm4x
2023-08-15 18:17:06 +02:00
parent 7b5dddfc06
commit b8fdb4202f
2 changed files with 29 additions and 0 deletions

View File

@@ -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')
@@ -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>

View File

@@ -6,6 +6,7 @@
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 = {
@@ -13,6 +14,12 @@
password: ''
};
onMount(() => {
if ($pocketbase.authStore.isValid) {
goto('/');
}
});
function tryAdminThenUser() {
$pocketbase.admins
.authWithPassword(form.email, form.password)