| 1 | <p align="center"> |
| 2 | <img alt="" |
| 3 | </p> |
| 4 | |
| 5 | <h1 align= |
| 6 | Better Auth Next.js Starter |
| 7 | </h1> |
| 8 | |
| 9 | <p align= |
| 10 | <a aria-label=" |
| 11 | <img alt="" |
| 12 | </a> |
| 13 | <img alt="" |
| 14 | <a aria-label=" |
| 15 | <img alt="" |
| 16 | </a> |
| 17 | </p> |
| 18 | |
| 19 | ## Quickstart |
| 20 | |
| 21 | 1) Create a PostgreSQL database (local or hosted). |
| 22 | |
| 23 | 2) Copy `.env.example` in the project root and fill in the variables below. |
| 24 | |
| 25 | 3) Generate schema and run database migrations. |
| 26 | |
| 27 | ```bash |
| 28 | bun run auth:generate |
| 29 | bun run db:generate |
| 30 | bun run db:push |
| 31 | ``` |
| 32 | |
| 33 | 4) Start the dev server. |
| 34 | |
| 35 | ```bash |
| 36 | bun run dev |
| 37 | ``` |
| 38 | |
| 39 | |
| 40 | ## Environment variables |
| 41 | |
| 42 | All configuration is typed and validated in `src/env.ts`. Requiredness differs between development and production as described. |
| 43 | |
| 44 | ```bash |
| 45 | # Runtime (server-only) |
| 46 | |
| 47 | # PostgreSQL connection string (required) |
| 48 | # Format must be a valid URL |
| 49 | DATABASE_URL="postgresql://postgres:<YOUR_PASSWORD>@localhost:5432/starter" |
| 50 | |
| 51 | # Better Auth secret (required in production; optional in development) |
| 52 | # You can generate via: openssl rand -base64 32 |
| 53 | # Docs: https://www.better-auth.com/docs/installation#set-environment-variables |
| 54 | BETTER_AUTH_SECRET="" |
| 55 | |
| 56 | # CORS allowed origins for auth endpoints |
| 57 | # Accepts a comma-separated list OR a JSON array of URLs |
| 58 | # Examples: |
| 59 | # CORS_ORIGIN="http://localhost:3000,https://your-domain.com" |
| 60 | # CORS_ORIGIN='["http://localhost:3000","https://your-domain.com"]' |
| 61 | CORS_ORIGIN="http://localhost:3000" |
| 62 | |
| 63 | # Runtime (client-exposed) |
| 64 | |
| 65 | # Public base URL of your site (required) |
| 66 | # Must be a full URL including protocol |
| 67 | # Example (dev): http://localhost:3000 |
| 68 | # Example (prod): https://your-domain.com |
| 69 | NEXT_PUBLIC_BASE_URL=http://localhost:3000 |
| 70 | |
| 71 | |
| 72 | # Build-time / tooling (optional) |
| 73 | |
| 74 | # Enable bundle analyzer for production builds |
| 75 | ANALYZE=false |
| 76 | |
| 77 | # Emit production browser source maps |
| 78 | SOURCE_MAPS=false |
| 79 | ``` |
| 80 | |
| 81 | Notes |
| 82 | - `CORS_ORIGIN` may be a single URL, a comma-separated list, or a JSON array. All values must be valid URLs. |
| 83 | - `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. |
| 84 | - `NEXT_PUBLIC_BASE_URL` is used for authentication, metadata, and sitemap generation. It must be the publicly reachable URL of your app. |
| 85 | |
| 86 | |
| 87 | ## Database and migrations |
| 88 | |
| 89 | This starter uses Drizzle ORM with PostgreSQL. Common commands: |
| 90 | |
| 91 | ```bash |
| 92 | # Generate SQL from schema |
| 93 | bun run db:generate |
| 94 | |
| 95 | # Apply migrations |
| 96 | bun run db:push |
| 97 | |
| 98 | # Open Drizzle Studio |
| 99 | bun run db:studio |
| 100 | ``` |
| 101 | |
| 102 | |
| 103 | ## Features |
| 104 | |
| 105 | [Better Auth](https://better-auth.com) |
| 106 | |
| 107 | [Better Auth UI](https://better-auth-ui.com) |
| 108 | |
| 109 | [shadcn/ui](https://ui.shadcn.com) |
| 110 | |
| 111 | [TailwindCSS](https://tailwindcss.com) |
| 112 | |
| 113 | [Drizzle ORM](https://orm.drizzle.team) |
| 114 | |
| 115 | [PostgreSQL](https://postgresql.org) |
| 116 | |
| 117 | [Biome](https://biomejs.dev) |
| 118 | |
| 119 | [Next.js](https://nextjs.org) |
| 120 | |
| 121 | ## Deploy on Vercel |
| 122 | |
| 123 | The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme). |
| 124 | |
| 125 | After creating a project, set the environment variables from the section above in the Vercel project settings and redeploy. |