Choosing a stack
All three options below give you email + password auth with server-managed sessions — the difference is where sessions and rows live.
| Stack | Session storage | Best fit |
|---|---|---|
| Better Auth + Neon | Signed cookie, sessions table in your own Postgres | Full control over schema and auth logic, no vendor lock-in. |
| Supabase Auth | Managed by Supabase, enforced via Row Level Security | Already using Supabase Postgres — auth and data share one project. |
| Custom (raw SQL) | Whatever you build — usually a cookie + sessions table | Learning 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.
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.
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))session.user.id yourself.Minimal schema
| Table | Key columns |
|---|---|
users | id, email (unique), name, created_at |
accounts | user_id, password_hash, provider |
sessions | id, user_id, expires_at, token |
Security checklist
- Hash passwords with bcrypt/scrypt/Argon2 — never MD5, SHA-1, or plaintext.
- Set the session cookie
httpOnlyandsecureso 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.
