Skip to content

techwithanirudh/better-auth-nextjs-starter

Repository files navigation

Better Auth Next.js Starter

Quickstart

  1. Create a PostgreSQL database (local or hosted).

  2. Copy .env.example in the project root and fill in the variables below.

  3. Generate schema and run database migrations.

bun run auth:generate
bun run db:generate
bun run db:push
  1. Start the dev server.
bun run dev

Environment variables

All configuration is typed and validated in src/env.ts. Requiredness differs between development and production as described.

# Runtime (server-only)

# PostgreSQL connection string (required)
# Format must be a valid URL
DATABASE_URL="postgresql://postgres:<YOUR_PASSWORD>@localhost:5432/starter"

# Better Auth secret (required in production; optional in development)
# You can generate via: openssl rand -base64 32
# Docs: https://www.better-auth.com/docs/installation#set-environment-variables
BETTER_AUTH_SECRET=""

# CORS allowed origins for auth endpoints
# Accepts a comma-separated list OR a JSON array of URLs
# Examples:
# CORS_ORIGIN="http://localhost:3000,https://your-domain.com"
# CORS_ORIGIN='["http://localhost:3000","https://your-domain.com"]'
CORS_ORIGIN="http://localhost:3000"

# Google Gemini API key (required)
GOOGLE_GENERATIVE_AI_API_KEY=your_gemini_api_key_here


# Runtime (client-exposed)

# Public base URL of your site (required)
# Must be a full URL including protocol
# Example (dev): http://localhost:3000
# Example (prod): https://your-domain.com
NEXT_PUBLIC_BASE_URL=http://localhost:3000


# Build-time / tooling (optional)

# Enable bundle analyzer for production builds
ANALYZE=false

# Emit production browser source maps
SOURCE_MAPS=false

Notes

  • CORS_ORIGIN may be a single URL, a comma-separated list, or a JSON array. All values must be valid URLs.
  • BETTER_AUTH_SECRET is mandatory in production. In development it can be left empty to use the library defaults, but setting it early avoids surprises.
  • NEXT_PUBLIC_BASE_URL is used for authentication, metadata, and sitemap generation. It must be the publicly reachable URL of your app.

Database and migrations

This starter uses Drizzle ORM with PostgreSQL. Common commands:

# Generate SQL from schema
bun run db:generate

# Apply migrations
bun run db:push

# Open Drizzle Studio
bun run db:studio

Features

Better Auth

Better Auth UI

shadcn/ui

TailwindCSS

Drizzle ORM

PostgreSQL

Biome

Next.js

Deploy on Vercel

The easiest way to deploy your Next.js app is to use the Vercel Platform.

After creating a project, set the environment variables from the section above in the Vercel project settings and redeploy.

About

Better Auth Next.js Starter

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published