Better Auth
TypeScript-first authentication for Next.js, Express, and React. Better Auth handles sessions, credentials, OAuth, and more.
Better Auth is a framework-agnostic TypeScript authentication library. StackKit sets it up with the right configuration for your framework so you can start authenticating users immediately.
Compatible frameworks
Better Auth works across all three StackKit frameworks:
| Framework | Notes |
|---|---|
| Next.js | API routes, server-side session access, React hooks |
| Express | Mounted as a middleware with dedicated auth routes |
| React | Client-side session management and sign-in/sign-up flows |
When using Better Auth with Express, a database is required. Better Auth needs somewhere to persist sessions and user records.
Installation
In a new project
npx stackkit@latest create my-app --auth better-authpnpm dlx stackkit@latest create my-app --auth better-authyarn dlx stackkit@latest create my-app --auth better-authbun x stackkit@latest create my-app --auth better-authIn an existing project
npx stackkit@latest add
# Navigate to: Authentication → Better AuthWhat gets generated
StackKit writes all the files Better Auth needs — you don't have to touch any configuration manually.
Next.js:
lib/auth.ts— Core auth configuration (plugins, social providers, etc.)lib/auth-client.ts— Client-side hooks (useSession, signIn, signOut)app/api/auth/[...all]/route.ts— API handler that mounts Better Auth
Express:
src/lib/auth.ts— Auth instance with database adaptersrc/routes/auth.routes.ts— Express router for auth endpoints
React:
src/lib/auth-client.ts— Client configured for your backend URL
Setup
Copy your environment file
cp .env.example .envGenerate a secret key
Better Auth requires a secret for signing sessions. Generate a strong one:
openssl rand -base64 32Copy the output into your .env file.
Set your environment variables
Open .env and fill in:
BETTER_AUTH_SECRET="paste-your-generated-secret-here"
BETTER_AUTH_URL="http://localhost:3000"For Express projects, use http://localhost:5000.
BETTER_AUTH_URL is the base URL of your application. In production, this should be your actual domain.
Run the database migration (if applicable)
If your project includes Prisma, Better Auth adds user and session tables to your schema. Apply them with:
npx prisma migrate dev --name better-auth-initStart the development server
npm run devEnvironment variables
| Variable | Required | Description |
|---|---|---|
BETTER_AUTH_SECRET | Yes | Secret key for signing session tokens. Must be at least 32 characters. |
BETTER_AUTH_URL | Yes | Base URL of your app (e.g., http://localhost:3000 for Next.js/React, http://localhost:5000 for Express). |
Customizing your auth setup
The generated lib/auth.ts (or src/lib/auth.ts for Express) is where you configure Better Auth. You can add social providers, enable 2FA, set session duration, and more — all by editing this single file.
For example, to add Google OAuth:
import { betterAuth } from "better-auth";
export const auth = betterAuth({
// ... existing config
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
},
},
});For the full list of options, see the Better Auth documentation.
Next steps
- Troubleshooting — Common auth-related issues