A TanStack Start example demonstrating authentication patterns and protected routes.
To start a new project based on this example, run:
npx gitpick TanStack/router/tree/main/examples/react/start-basic-auth start-basic-authFrom your terminal:
pnpm install
make db-push-dev # applies the schema to the dev database
make dev # boots docker, waits for Postgres, and runs pnpm devThis starts your app in development mode, rebuilding assets on file changes. Use make test / make db-push-test to target the test database defined in docker-compose.yml.
To build the app for production:
pnpm build- The example now uses Drizzle ORM with the PostgreSQL (
pg) driver.src/db/schema.tsre-exports the Better Auth schema generated insrc/db/better-auth-schema.ts. If you customize the auth options, regenerate it withpnpm exec better-auth generate --output src/db/better-auth-schema.ts --yes. - Environment-specific settings live in
.env.developmentand.env.test. The Makefile loads the appropriate file automatically for thedev,test,db-push-*, and log/down targets, so no manualdotenvprefixes are needed. - Better Auth requires a high-entropy
BETTER_AUTH_SECRETand the publicBETTER_AUTH_URLfor callbacks. Copy the sample values in the.env.*files and replace them before deploying. make db-push-dev/make db-push-testrun the schema against thedb-dev(postgresql://postgres:postgres@localhost:5433/tracker_dev) anddb-test(postgresql://postgres:postgres@localhost:5434/tracker_test) containers respectively.
src/server/auth.tswires Better Auth to Drizzle via the official adapter and automatically syncs cookies via the TanStack Start integration.- Login and signup forms call the exported
loginFn/signupFnserver functions that proxy toauth.api.signInEmail/signUpEmail, so sessions and cookies are handled for you. - Sign-out uses the same API (
auth.api.signOut), and the root route fetches the current session viaauth.api.getSessionto hydrate route context.
This example demonstrates:
- User authentication flow
- Protected routes
- Login/logout functionality
- Redirect after authentication