devtools logo
by Ryan Macalandag
Back to cheatsheets
Cheatsheetsreactnextjsauthsupabaseneon

React / Next.js Auth Cheatsheet

Auth patterns with Supabase, Neon, email, and SQL.

Choosing a stack

All three options below give you email + password auth with server-managed sessions — the difference is where sessions and rows live.

StackSession storageBest fit
Better Auth + NeonSigned cookie, sessions table in your own PostgresFull control over schema and auth logic, no vendor lock-in.
Supabase AuthManaged by Supabase, enforced via Row Level SecurityAlready using Supabase Postgres — auth and data share one project.
Custom (raw SQL)Whatever you build — usually a cookie + sessions tableLearning exercise or highly specific requirements. Most apps shouldn't roll their own.

Email + password flow

The shape is the same regardless of provider: verify credentials server-side, then hand the browser an opaque, unguessable session identifier.

Sign-up form

client submits email + password

Server verifies

hashes password, checks against stored hash

Session created

row in sessions table + signed httpOnly cookie

Protected page

reads cookie → looks up session → loads user

Reading the session

Read the session inside a Server Component or Server Action — never trust a session value passed from the client.

Server Component
import { headers } from 'next/headers'
import { auth } from '@/lib/auth'

export default async function DashboardPage() {
  const session = await auth.api.getSession({ headers: await headers() })
  if (!session) redirect('/auth/sign-in')

  return <p>Welcome, {session.user.name}</p>
}

Protecting routes

Check the session as close to the data access as possible — a proxy/middleware check is a good first gate, but the final read/write should re-check on the server.

Scoping a query to the current user
const session = await auth.api.getSession({ headers: await headers() })
if (!session) redirect('/auth/sign-in')

// Always filter by the session's user id — there is no RLS on Neon.
const orders = await db
  .select()
  .from(ordersTable)
  .where(eq(ordersTable.userId, session.user.id))

Minimal schema

TableKey columns
usersid, email (unique), name, created_at
accountsuser_id, password_hash, provider
sessionsid, user_id, expires_at, token

Security checklist

  • Hash passwords with bcrypt/scrypt/Argon2 — never MD5, SHA-1, or plaintext.
  • Set the session cookie httpOnly and secure so client JS and plain HTTP can't read it.
  • Rate-limit sign-in and sign-up endpoints to slow down credential stuffing.
  • Scope every database query by the authenticated user's id — don't trust an id passed in the request body.
  • Expire sessions server-side and let the user revoke active sessions from a settings page.

Spot an error or have a suggestion? Open an issue on GitHub.