| name | better-auth-sveltekit |
| description | How to plan, install, and integrate Better Auth inside SvelteKit apps, including CLI/API usage, cookies/session models, Convex DB or traditional databases (MongoDB, Postgres, SQLite), and client wiring. Use when adding/updating auth flows, providers, or DB adapters in this repo or other SvelteKit projects. |
Better Auth + SvelteKit
Overview
This skill encapsulates the Better Auth docs (introduction, concepts, SvelteKit integration) plus Convex/DB guidance so you can bootstrap secure authentication quickly. Pair it with the sveltekit2 + svelte5 skills for general Svelte workflows.
Quick start
- Review Better Auth concepts (API, CLI, client, cookies, sessions, rate limits, hooks/plugins) in
references/setup-and-cli.md. - Pick a storage strategy (Convex, Mongo, Postgres/SQLite via Prisma/Drizzle) using
references/databases-and-storage.md. - Initialize Better Auth (CLI or manual config), create a server instance under
src/lib/server/auth.ts, and register it inhooks.server.ts. - Follow
references/sveltekit-integration.mdto wire server loads, actions, remote functions, and client helpers. - Configure providers (email/password, OAuth) plus emails, plugins, and custom hooks via
references/api-and-client-concepts.md. - Exercise flows locally with the Better Auth CLI dev server and your Vitest/Playwright suites.
Workflow
- Plan auth surface
- Enumerate flows (signup/login/passwordless/OAuth), environments, and domain requirements (cookies + CSP).
- Decide where user metadata lives (Better Auth default tables vs Convex/DB) and how to sync it.
- Bootstrap Better Auth
- Install packages:
better-auth,@better-auth/client,better-auth/sveltekit, provider/database adapters, plus Convex client if needed. - Run
npx better-auth init(or copybetter-auth.config.{ts,js}) and set secrets in.env(JWT, encryption keys, SMTP/OAuth creds). - Familiarize yourself with CLI commands (
better-auth dev,better-auth routes,better-auth doctor) viareferences/setup-and-cli.md.
- Install packages:
- Create the server instance
- Define
src/lib/server/auth.ts:import { betterAuth } from "better-auth/sveltekit"; import { convexAdapter } from "@better-auth/adapter-convex"; // or prisma/mongo export const auth = betterAuth({ database: convexAdapter({ url: process.env.CONVEX_URL }), email: { provider: "resend", from: "auth@example.com" }, oauth: { providers: [/* github, google, etc. */] }, session: { rolling: true, cookieName: "session", sameSite: "lax" } }); - Register in
hooks.server.ts:import { auth } from "$lib/server/auth"; export const handle = auth.handleHooks(); export const handleFetch = auth.handleFetch; - Expose helpers (
locals.session,locals.user,locals.auth) for load/actions.
- Define
- Wire SvelteKit routes
- In
+layout.server.ts, readlocals.auth.validate()to gate protected pages and redirect guests. - Implement actions or remote functions for login/signup by calling
auth.api.signInPassword(server) or using the browser client. - Use
depends("auth:session")+invalidate("auth:session")to keep UI reactive after auth mutations. - Provide error boundaries and user-friendly messaging per repo conventions.
- In
- Client usage
- Instantiate the client side helper once:
import { createBetterAuthClient } from "@better-auth/client"; export const authClient = createBetterAuthClient({ baseURL: "/api/auth", fetch: window.fetch }); - Use runes to track session state, call
authClient.signInPassword,signOut,getSession. - For Convex, sync tokens via Better Auth session hooks so Convex client can verify identity.
- Instantiate the client side helper once:
- Database + infrastructure
- Follow adapter-specific steps: Convex functions, Mongo collections (indexes on emails + sessions), Postgres/SQLite via Prisma migrations.
- Configure email delivery, hooks (before/after user created), plugins, rate limits, and custom claims as described in
references/api-and-client-concepts.md.
- Test & operate
- Run CLI dev server locally, seed test users via
better-auth cli users:create. - Cover flows with Vitest/Playwright; mock SMTP/OAuth providers.
- Monitor cookies/session expiration, rotate secrets, and document any custom hooks.
- Run CLI dev server locally, seed test users via
References
references/setup-and-cli.md— installation, CLI workflow, API surface, cookies/sessions, rate limits, and troubleshooting.references/sveltekit-integration.md— SvelteKit-specific wiring for hooks, load/actions, remote functions, and client helpers.references/databases-and-storage.md— Convex DB bridge plus MongoDB/Postgres/SQLite adapters and migration tips.references/api-and-client-concepts.md— deep dive into Better Auth API/client, hooks, plugins, OAuth, email, and user/account management.