Users and Auth
SveltePress comes with some useful features for handling users and auth. PocketBase does the heavy lifting on the backend for both.
User
In the server context, the user is available in locals:
+page.server.ts / +layout.server.ts
tsexport const load = async ({ locals }) => {const { user } = locals;};
tsexport const load = async ({ locals }) => {const { user } = locals;};
In the universal context, the user is passed down from the root layout:
routes/+layout.ts
tsexport const load = async (event) => {const pb = new SveltePress({fetch: event.fetch,});return {pb,user: pb.authStore.model,};};
tsexport const load = async (event) => {const pb = new SveltePress({fetch: event.fetch,});return {pb,user: pb.authStore.model,};};
To access it in other pages or layouts, use the await parent() call:
+page.ts / +layout.ts
tsexport const load = async ({ parent }) => {const { user } = await parent();};
tsexport const load = async ({ parent }) => {const { user } = await parent();};
Sign In
routes/signin/+page.server.ts
tsexport const actions = {default: async ({ locals, cookies }) => {const { pb } = locals;try {await pb.collection("users").authWithPassword(form.data.email, form.data.password);} catch (e) {throw error(500);}pb.authStore.saveCookie(cookies);throw redirect("/dashboard");},};
tsexport const actions = {default: async ({ locals, cookies }) => {const { pb } = locals;try {await pb.collection("users").authWithPassword(form.data.email, form.data.password);} catch (e) {throw error(500);}pb.authStore.saveCookie(cookies);throw redirect("/dashboard");},};
Sign Out
routes/signout/+page.server.ts
tsexport const load = async ({ locals, cookies }) => {const { pb } = locals;pb.authStore.clearCookie(cookies);throw redirect("/");};
tsexport const load = async ({ locals, cookies }) => {const { pb } = locals;pb.authStore.clearCookie(cookies);throw redirect("/");};
Protecting Pages
Using a route layout to protect pages, the user object will exist only when there is an active session:
routes/(authed)/+layout.ts
tsexport const load = async ({ parent }) => {const { user } = await parent();if (!user) {throw redirect("/signin");}};
tsexport const load = async ({ parent }) => {const { user } = await parent();if (!user) {throw redirect("/signin");}};
Refreshing Auth Tokens
Following the recommended practice for short lived auth tokens created at sign in
and then refreshed periodically during app usage, SveltePress comes with a setRefreshTokenInterval
helper function that can instantiated in the root layout:
routes/+layout.svelte
svelte<script>import { onMount } from "svelte";import { setRefreshTokenInterval } from "sveltepress";onMount(() => {const interval = setRefreshTokenInterval();return () => clearInterval(interval);});</script>
svelte<script>import { onMount } from "svelte";import { setRefreshTokenInterval } from "sveltepress";onMount(() => {const interval = setRefreshTokenInterval();return () => clearInterval(interval);});</script>
The default refreshes every hour and can be configured:
tsconst interval = setRefreshTokenInterval(1000 * 60 * 60);
tsconst interval = setRefreshTokenInterval(1000 * 60 * 60);