Skip to main content
Tutorials

Better Auth in Next.js 16: the full production setup

Beyond the login form: the production-ready Better Auth setup in Next.js 16 — 2FA, organizations, admin, rate limiting, and the gotchas that matter.

Israel Palma Sebastià

This blog belongs to CREA.MBA, a Next.js boilerplate with auth, payments and an admin panel already set up. See what's included

Better Auth is the reasonable default for new Next.js projects in 2026. But "set up auth" and "set up auth that survives production" are different jobs. A login form is an afternoon; sessions, 2FA, organizations, admin controls and the edge cases around them are a week.

This is the production starter setup — the pieces you actually need, and the gotchas between a demo and a real app.

The core setup

Better Auth runs inside your Next.js app against your own database. The minimum production-grade core:

  1. Server instance (auth) with your database adapter (Prisma + Postgres) and emailAndPassword + socialProviders (Google, GitHub, LinkedIn).
  2. Client instance for React with typed hooks.
  3. Session handling: httpOnly cookies, sensible expiry, and auth.api.getSession() in server components / route handlers.
  4. Email verification + password reset wired to your email provider (Resend) — not optional for production.

The plugins that turn it into a SaaS

This is where Better Auth pulls ahead. Each is a line of config, not a week of building:

  • twoFactor() — TOTP + backup codes. Don't ship a paid product without it.
  • organization() — multi-tenancy: teams, roles (owner/admin/member), invitations. The backbone of B2B SaaS.
  • admin() — impersonation, ban, list sessions. The support tooling you'll need by month two.
  • passkey() — WebAuthn, increasingly expected.
  • magicLink() — passwordless option.

Type safety flows from here: enable the organization plugin and session.activeOrganizationId becomes typed automatically.

The production gotchas

1. Verify email before granting access. A demo skips it; production gets fake signups. Gate sensitive routes on emailVerified.

2. Rate-limit auth endpoints. Login, signup and reset are abuse magnets. Better Auth has rate limiting — turn it on, don't assume defaults fit.

3. Session strategy on edge. Better Auth works on edge without a special adapter, but decide deliberately where sessions are read (middleware vs server component) to avoid double lookups.

4. Trusted origins / CSRF. Set trustedOrigins correctly for production domains, or OAuth callbacks and cross-site requests will fail in confusing ways.

5. Don't sprinkle auth checks. Centralize authorization in a helper (a createAuthenticatedAction pattern) instead of repeating getSession() guards across the codebase — it's where bugs and security holes hide.

What "production-ready" actually means

  • Email/password + OAuth, verified emails, password reset
  • 2FA with backup codes
  • Organizations with roles and invitations
  • Admin panel: impersonate, ban, sessions
  • Rate limiting, trusted origins, centralized auth guards
  • All typed end to end, all in your own database

That's roughly a week of careful work — and it's the same week for every SaaS you'll ever build.

Bottom line

The Better Auth core is quick. Production Better Auth — verification, 2FA, orgs, admin, rate limiting, the edge cases — is the part that eats a week and that you'll redo on every project. Build it once, well, and reuse it.


CREA.MBA is that production setup, done: Better Auth with email/password + OAuth, 2FA (TOTP + backup codes), organizations with roles and invitations, an admin panel with impersonation and bans, rate limiting and centralized auth actions — all on Prisma + your own Postgres. You start from the production checklist above already ticked, not from a login form.

Better AuthNext.jsAuthentication2FABoilerplate

Enjoyed this article?

Subscribe for more tutorials and tips on building products with AI