From 1f462a577ddde78120bf02f931446060176ef0e5 Mon Sep 17 00:00:00 2001 From: Micha Mailaender Date: Sat, 15 Nov 2025 08:47:07 +0100 Subject: [PATCH 1/2] docs(sveltekit): add authenticated requests guide with useQuery and expectAuth options --- .../docs/framework-guides/sveltekit.mdx | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/docs/content/docs/framework-guides/sveltekit.mdx b/docs/content/docs/framework-guides/sveltekit.mdx index 05c84ca..683b364 100644 --- a/docs/content/docs/framework-guides/sveltekit.mdx +++ b/docs/content/docs/framework-guides/sveltekit.mdx @@ -556,3 +556,52 @@ export const load: PageServerLoad = async ({ locals }) => { return { currentUser }; }; ``` + +### Authenticated requests + +There are two common ways to make authenticated Convex requests from Svelte components. + +#### Option 1: Conditionally run queries with `useQuery` and `auth.isAuthenticated` + +Use this when your app has a mix of public and members-only content. +You can read the auth state from `useAuth` and return `"skip"` for queries that should only run once the user is authenticated. + +```ts title="src/routes/+page.svelte" +import { api } from '$convex/_generated/api'; +import { useQuery } from 'convex-svelte'; +import { useAuth } from '@mmailaender/convex-better-auth-svelte/svelte'; + +const auth = useAuth(); + +// Only fetch once the user is authenticated +const memberOnlyPosts = useQuery( + api.posts.getMemberOnlyPosts, + () => (auth.isAuthenticated ? {} : 'skip') +); + +// Always fetched, regardless of auth state +const publicPosts = useQuery(api.posts.getPublicPosts, {}); +```` + +#### Option 2: Make all requests authenticated with `expectAuth` + +Use this when your app is essentially “members-only” and almost all data requires authentication. + +By enabling `expectAuth`, all Convex queries and mutations created through `createSvelteAuthClient` will: + +* automatically include the auth token, and +* not run until the user is authenticated. + +Unauthenticated users won’t trigger any Convex requests until they sign in. + +```ts title="src/routes/+layout.svelte" +import { createSvelteAuthClient } from '@mmailaender/convex-better-auth-svelte/svelte'; +import { authClient } from '$lib/auth-client'; + +createSvelteAuthClient({ + authClient, + options: { + expectAuth: true + } +}); +``` From 67b5b9968f863a6591772d976e6e5960e4e46e6d Mon Sep 17 00:00:00 2001 From: Micha Mailaender Date: Sat, 6 Dec 2025 10:55:09 +0100 Subject: [PATCH 2/2] docs: add SSR to SvelteKit guide --- .../docs/framework-guides/sveltekit.mdx | 126 ++++++++++++++++-- 1 file changed, 115 insertions(+), 11 deletions(-) diff --git a/docs/content/docs/framework-guides/sveltekit.mdx b/docs/content/docs/framework-guides/sveltekit.mdx index 683b364..63515b2 100644 --- a/docs/content/docs/framework-guides/sveltekit.mdx +++ b/docs/content/docs/framework-guides/sveltekit.mdx @@ -1,10 +1,10 @@ --- -title: Sveltekit -description: Install and configure Convex + Better Auth for Sveltekit. +title: SvelteKit +description: Install and configure Convex + Better Auth for SvelteKit. --- - Sveltekit support is currently community maintained, and relies on the + SvelteKit support is currently community maintained, and relies on the [@mmailaender/convex-better-auth-svelte](https://github.com/mmailaender/convex-better-auth-svelte) package. A complete working example is provided in the repo, and any issues can be reported there as well. @@ -353,12 +353,10 @@ You're now ready to start using Better Auth with Convex. ## Usage Check out the [Basic Usage](/basic-usage) guide for more information on general -usage. Below are usage notes specific to Sveltekit. +usage. Below are usage notes specific to SvelteKit. ### Using Better Auth from the client -Note: To add SSR support (`data.currentUser`) also add the `src/routes/+page.server.ts` file from the `Using Better Auth from the server` example. - ```svelte title="src/routes/+page.svelte" + + {@render children()} + ``` + + +
+ #### Prefetch user data + + For the best experience, also prefetch user data on the server to prevent content flash. + + ```ts title="src/routes/+layout.server.ts" + import type { LayoutServerLoad } from "./$types"; + import { api } from "$convex/_generated/api"; + import { createAuth } from "$convex/auth.js"; + import { createConvexHttpClient, getAuthState } from "@mmailaender/convex-better-auth-svelte/sveltekit"; + + export const load: LayoutServerLoad = async ({ locals, cookies }) => { + const authState = await getAuthState(createAuth, cookies); + + if (!authState.isAuthenticated) { + return { authState, currentUser: null }; + } + + const client = createConvexHttpClient({ token: locals.token }); + + try { + const currentUser = await client.query(api.auth.getCurrentUser, {}); + return { authState, currentUser }; + } catch { + return { authState, currentUser: null }; + } + }; + ``` + + Then use `initialData` in your queries to prevent data flash: + + ```ts title="src/routes/+page.svelte" + + ``` +
+ \ No newline at end of file