Better Auth integrates with SvelteKit through a server hook handler and a Svelte-aware auth client. Before you start, make sure you have a Better Auth instance configured. If you haven’t done that yet, check out the installation.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/better-auth/better-auth/llms.txt
Use this file to discover all available pages before exploring further.
Mount the handler
Register Better Auth inside SvelteKit’shooks.server.ts using svelteKitHandler from better-auth/svelte-kit. The handler intercepts requests to the auth base path and forwards them to Better Auth; all other requests are passed through to resolve.
hooks.server.ts
building flag prevents Better Auth from running during SvelteKit’s static build step.
Populate session data in locals
svelteKitHandler does not automatically populate event.locals. If you need access to the current user in server load functions, form actions, or endpoints, fetch the session and assign it in the handle hook:
hooks.server.ts
Declare the
session and user types on App.Locals in your src/app.d.ts file so TypeScript recognises them throughout your app.Server action cookies
To ensure cookies are set correctly when you call functions likesignInEmail or signUpEmail inside a server action, add the sveltekitCookies plugin to your auth instance.
getRequestEvent is available from SvelteKit 2.20.0 and later. Make sure you are on a compatible version.lib/auth.ts
Create a client
Create an auth client instance and export it from a shared module. Import frombetter-auth/svelte to get Svelte-specific reactive stores.
lib/auth-client.ts
useSession store
authClient.useSession() returns a nano-store. Subscribe to it with the $ prefix in Svelte components.
+page.svelte
Server-side session access in load functions
Because you populatedevent.locals in the hook, you can read the session directly from locals in any server load function without an additional database call.
routes/dashboard/+page.server.ts