This is an example of how to use Better Auth with SvelteKit.
Implements the following features:
- Email & Password Sign-in
- Social Sign-in with Google
- Sign Up
- Email Verification
- Password Reset
- form verification using Superforms
- all pages implemented using Svelte 5 Runes
- Clone the code sandbox (or the repo) and open it in your code editor
- Move .env.example to .env and provide necessary variables. For your Google sign in credentials see https://www.better-auth.com/docs/authentication/google
- Run the following commands
pnpm install npx drizzle-kit generate --name='Better-Auth-schema' npx drizzle-kit migrate pnpm dev
- Open the browser and navigate to
http://localhost:5173
If you want to use a different database, say Postgres then do the following:
- Edit the database url in
.env
- Change
/src/lib/server/db/index.ts
to use a Postgres database per the Drizzle documentation. - Change
/src/lib/server/auth.ts
to use the postgres provider. - Change the dialect in
/drizzle.config.ts
to postgres. - Generate a new auth schema using
npx @better-auth/cli generate
- Replace the contents of
/src/lib/server/db/schema.ts
with the generated schema. - Delete folder
/src/drizzle
- Run the drizzle generate and migrate:
npx drizzle-kit generate --name='Better-Auth-schema' npx drizzle-kit migrate
The SvelteCookies plugin was implemented to fix this known issue. The code for the plugin was taken from this gist courtesy of @actuallyjamez
My dev server kept throwing 404 errors while using Chrome in dev mode. vite-plugin-devtools-json provided a way to prevent that.
I ran into some issues using runes with Svelte 5 and Superforms. This sveltekit-superforms issue provided guidance.