freeauthv0.1.0
Better Auth
Better Auth with email/password plus your pick of GitHub, Google and Discord, database sessions in Postgres through Prisma, admin roles, route protection and two-factor auth.
$
npx cnippet-stack init better-authWant to understand the code? Read the step-by-step guide on next.cnippet.dev →
supports
- Next.js
- 16
- Router
- app
- ORM
- prisma
Requires: prisma
prompts
- Which OAuth providers? (email and password is always on)(multiselect)GitHubGoogleDiscord
- Add sign-in, register, two-factor, dashboard, admin and profile pages?(confirm)
environment variables
DATABASE_URLPostgreSQL connection string, read by the Prisma client the auth tables live in.BETTER_AUTH_SECRETSecret that signs session cookies. At least 32 characters.BETTER_AUTH_URLYour app's URL. Better Auth builds OAuth callback URLs from it and only trusts requests from it.GITHUB_CLIENT_IDGitHub OAuth client ID, from github.com/settings/developers.GITHUB_CLIENT_SECRETGitHub OAuth client secret, from github.com/settings/developers.GOOGLE_CLIENT_IDGoogle OAuth client ID, from console.cloud.google.com.GOOGLE_CLIENT_SECRETGoogle OAuth client secret, from console.cloud.google.com.DISCORD_CLIENT_IDDiscord OAuth client ID, from discord.com/developers/applications.DISCORD_CLIENT_SECRETDiscord OAuth client secret, from discord.com/developers/applications.
manual steps
- Migrate the auth tablesrequired
Run `prisma migrate dev --name better-auth` to create the auth tables.
- Create the OAuth appsrequired
Create an OAuth app for each provider you picked, with the callback URL `<BETTER_AUTH_URL>/api/auth/callback/<provider>` (e.g. `http://localhost:3000/api/auth/callback/github`), then set its CLIENT_ID and CLIENT_SECRET.