diff --git a/docs/content/docs/framework-guides/sveltekit.mdx b/docs/content/docs/framework-guides/sveltekit.mdx index 2702fec0..2470fe2f 100644 --- a/docs/content/docs/framework-guides/sveltekit.mdx +++ b/docs/content/docs/framework-guides/sveltekit.mdx @@ -10,6 +10,9 @@ description: Install and configure Convex + Better Auth for SvelteKit. can be reported there as well. +This guide targets SvelteKit 3. Still on SvelteKit 2? Follow along and apply the +differences listed in [SvelteKit 2](#sveltekit-2). + ## Prerequisites You'll first need a project where Convex is already set-up. Ensure that all @@ -22,7 +25,7 @@ steps in prerequisites are completed. ### Install convex ```npm -npm install convex @mmailaender/convex-svelte +npm install convex convex-svelte ``` @@ -54,32 +57,18 @@ npm install convex @mmailaender/convex-svelte
- ### Add $convex alias - - Add the following to your `svelte.config.js` file: - - ```ts title="svelte.config.js" - import adapter from '@sveltejs/adapter-auto'; - import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'; - - /** @type {import('@sveltejs/kit').Config} */ - const config = { - // Consult https://svelte.dev/docs/kit/integrations - // for more information about preprocessors - preprocess: vitePreprocess(), - - kit: { - // adapter-auto only supports some environments, see https://svelte.dev/docs/kit/adapter-auto for a list. - // If your environment is not supported, or you settled on a specific environment, switch out the adapter. - // See https://svelte.dev/docs/kit/adapters for more information about adapters. - adapter: adapter(), - alias: { - $convex: './src/convex' - } - } - }; + ### Add a `#convex` import alias - export default config; + Add a [subpath import](https://svelte.dev/docs/kit/$lib) to your `package.json`, so you can import from `src/convex/` as `#convex/...`: + + ```json title="package.json" + { + "imports": { + "#lib": "./src/lib/index.js", + "#lib/*": "./src/lib/*", + "#convex/*": "./src/convex/*" // [!code ++] + } + } ```
@@ -160,9 +149,6 @@ npm install convex @mmailaender/convex-svelte PUBLIC_CONVEX_URL=https://adjective-animal-123.convex.cloud - # Same as PUBLIC_CONVEX_URL but ends in .site // [!code ++] - PUBLIC_CONVEX_SITE_URL=https://adjective-animal-123.convex.site # [!code ++] - # Your local site URL // [!code ++] PUBLIC_SITE_URL=http://localhost:5173 # [!code ++] ``` @@ -181,6 +167,31 @@ npm install convex @mmailaender/convex-svelte PUBLIC_SITE_URL=http://localhost:5173 # [!code ++] ``` + On Convex Cloud, the Convex site URL (`.convex.site`) is derived from `PUBLIC_CONVEX_URL`. + Self-hosted, local, and custom-domain deployments need `PUBLIC_CONVEX_SITE_URL`. + + Declare the variables in `src/env.ts`. SvelteKit 3 only exposes declared variables. + + ```ts title="src/env.ts" tab="Cloud" + import { defineEnvVars } from '@sveltejs/kit/env'; + + export const variables = defineEnvVars({ + PUBLIC_CONVEX_URL: { public: true, static: true } + }); + ``` + + ```ts title="src/env.ts" tab="Self hosted" + import { defineEnvVars } from '@sveltejs/kit/env'; + + export const variables = defineEnvVars({ + PUBLIC_CONVEX_URL: { public: true, static: true }, + PUBLIC_CONVEX_SITE_URL: { public: true, static: true } + }); + ``` + + `static: true` inlines the values at build time. Keep it if the values only + exist while building, for example with `npx convex deploy --cmd 'npm run build'`. +
@@ -267,26 +278,48 @@ npm install convex @mmailaender/convex-svelte Set up route handlers to proxy auth requests from your framework server to your Convex deployment. - ```ts title="src/routes/api/auth/[...all]/+server.ts" + ```ts title="src/routes/api/auth/[...all]/+server.ts" tab="Cloud" import { createSvelteKitHandler } from '@mmailaender/convex-better-auth-svelte/sveltekit'; export const { GET, POST } = createSvelteKitHandler(); ``` + ```ts title="src/routes/api/auth/[...all]/+server.ts" tab="Self hosted" + import { createSvelteKitHandler } from '@mmailaender/convex-better-auth-svelte/sveltekit'; + import { PUBLIC_CONVEX_SITE_URL } from '$app/env/public'; + + export const { GET, POST } = createSvelteKitHandler({ convexSiteUrl: PUBLIC_CONVEX_SITE_URL }); + ``` + +
+ +
+ ### Initialize the Convex client + + Register your deployment URL once in the universal hooks file, which runs on the server and in the browser. + `createSvelteAuthClient`, `createConvexHttpClient`, and `createSvelteKitHandler` all read it from there. + + ```ts title="src/hooks.ts" + import { initConvex } from 'convex-svelte/sveltekit'; + import { PUBLIC_CONVEX_URL } from '$app/env/public'; + + initConvex(PUBLIC_CONVEX_URL); + ``` +
### Set up Convex client provider Initialize the Convex client with auth in your app. - Note: `createSvelteAuthClient` includes already `setupConvex()` from `@mmailaender/convex-svelte`. + Note: `createSvelteAuthClient` already calls `setupConvex()` from `convex-svelte`, reusing the client from `initConvex()`. ```ts title="src/routes/+layout.svelte" + + {#if auth.isAuthenticated} + {@render children()} + {:else if auth.isLoading} +

Loading…

+ {:else} +

Please sign in.

+ {/if} + ``` + +3. **Pass the server's auth state** (see [SSR](#ssr-optional)). Signed-in + users then render the area on the first paint, and their first queries are + already authenticated. -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"; + + These steps shape what users see. They don't protect your data: every Convex + function that returns private data must check the user itself, e.g. with + `authComponent.getAuthUser(ctx)`. + -createSvelteAuthClient({ - authClient, - options: { - expectAuth: true, - }, -}); -``` + + Other guides use Convex's `expectAuth` client option for this. It only holds + requests until the first sign-in, and leaves signed-out users with a client + that never sends anything, so this guide doesn't use it. + ### SSR (Optional) @@ -659,7 +755,7 @@ load. ```ts title="src/routes/+layout.svelte"