0
Challenge · 31Mutations & Auth
The 3-layer auth gate
Securityused in ~75% of apps
Almost every "I secured my app" bug is the same bug: someone put the auth check in one place — usually proxy.ts, or worse, a conditional in the UI — and called it done. A real Next.js engineer knows a protected feature needs three independent checks, because an attacker doesn't click your buttons; they POST straight at your server.
Core idea. Authorization in Next.js is defense-in-depth across three layers: (1) proxy.ts does an optimistic redirect so logged-out users don't see the page; (2) every Server Action / Route Handler re-checks the real session itself, because a client can POST directly and skip the proxy; (3) every data query is scoped by owner/tenant, so an authenticated user still can't touch another user's rows. Hiding a button on the client protects nothing.
poke it, drag it,
try to break it
try to break it
no slides here.
you drive.
you drive.
Playground, this isn't a screenshot
<Link> nav
myapp.com/
<Home />
Public page — anyone can open this, logged in or not.
You are logged out. Click a locked page and watch the guard redirect you.
route needs auth redirected to /login sent back to intended page
↑ play a little first, then dig in