This is an example of implementing authentication in Waku using better-auth.
npm create waku@latest
cd waku-project
npm install --save --exact better-auth better-sqlite3
npm install --save-dev --exact @types/better-sqlite3Copy .env-example to .env and create a BETTER_AUTH_SECRET
Create auth.ts with better-auth server config.
Generate the database schema:
npm exec @better-auth/cli generateRun the database migrations:
npm exec @better-auth/cli migrateCreate middleware for Waku to load the session from the request headers and store it in Waku's context data.
Add middleware to waku.config.ts:
import { defineConfig } from "waku/config";
export default defineConfig({
middleware: [
"waku/middleware/context",
"waku/middleware/dev-server",
"./src/middleware/auth.ts",
"waku/middleware/handler",
],
});Set up auth API routes and forward them to better-auth.
Create a better-auth client and use it in client components.
Read the session from Waku context data in server components: