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-auth

Want 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_URL PostgreSQL connection string, read by the Prisma client the auth tables live in.
  • BETTER_AUTH_SECRET Secret that signs session cookies. At least 32 characters.
  • BETTER_AUTH_URL Your app's URL. Better Auth builds OAuth callback URLs from it and only trusts requests from it.
  • GITHUB_CLIENT_ID GitHub OAuth client ID, from github.com/settings/developers.
  • GITHUB_CLIENT_SECRET GitHub OAuth client secret, from github.com/settings/developers.
  • GOOGLE_CLIENT_ID Google OAuth client ID, from console.cloud.google.com.
  • GOOGLE_CLIENT_SECRET Google OAuth client secret, from console.cloud.google.com.
  • DISCORD_CLIENT_ID Discord OAuth client ID, from discord.com/developers/applications.
  • DISCORD_CLIENT_SECRET Discord 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.