Minimal starter for SvelteKit + Convex + Better Auth with:
name / email / password),/(app) route group,src/hooks.server.ts,Better Auth handles login, registration, and session.Convex is the backend and user data source.SvelteKit renders the UI and enforces server-side guards.withServerConvexToken(...) in src/hooks.server.ts passes the token to SSR, so server-side Convex queries see the logged-in user.npm install
Set required environment variables for auth/Convex if you don't have them yet.
Most commonly needed:
SITE_URL — the app URL used by Better Auth,CONVEX_DEPLOYMENT / Convex URL — depending on your local Convex setup,Start the app:
npm run dev
npx convex dev
src/convex/auth.ts — Better Auth config on the Convex side.src/convex/auth.config.ts — auth providers config for Convex.src/convex/http.ts — HTTP endpoints for auth/integration.src/lib/auth-client.ts — Better Auth client used in Svelte on the frontend.src/hooks.server.ts — the central point:event.locals.token,/(app) route group,withServerConvexToken(...).src/routes/+layout.server.ts — fetches SSR auth state via getAuthState().src/routes/+layout.svelte — sets up createSvelteAuthClient(...) and passes authState to the client.src/routes/+page.svelte — public landing with a login button.src/routes/login/+page.svelte — minimal login form.src/routes/register/+page.svelte — minimal registration form.src/routes/(app)/app/+page.server.ts — example of fetching user data on the server.src/routes/(app)/app/+page.svelte — protected view after login./.Login and goes to /login./app page.src/hooks.server.ts redirects from /(app) to /login./login or /register, gets redirected to /app./(app) route group is for organization and protection only — it doesn't change the public URL.+page.server.ts, not in the client component.authClient.signIn.email(...), authClient.signUp.email(...), and authClient.signOut() are only called on the client in event handlers.src/hooks.server.ts.src/
convex/
auth.ts
auth.config.ts
http.ts
lib/
auth-client.ts
routes/
+page.svelte
+layout.server.ts
+layout.svelte
login/
register/
(app)/app/