Skip to content
Loadout

Fix

Supabase SSR: getClaims, not getSession, on the server

Answer

Use supabase.auth.getClaims() to protect server pages and data. Never trust getSession() for authorization: it reads the cookie without revalidating it, and anyone can forge that cookie.

  • Next.js 16.4.5
  • React 19.3
  • @supabase/ssr 0.12.7
  • @supabase/supabase-js current, installed alongside @supabase/ssr

Last verified 2026-11-04

The Next.js + Supabase pack pins Next.js 16.4.5 and @supabase/ssr 0.12.7. In server code such as proxy.ts, getSession() reads the session from the cookie without revalidating it. A forged cookie must never decide who can read a page or write data. Never authorize with session.user.

Use getClaims() to protect pages and data. It verifies the JWT signature on every call. With asymmetric signing keys, the default for new projects, verification happens locally against cached JWKS. With symmetric keys, it calls the Auth server. getClaims() also refreshes the session when the access token is close to expiring.

Use getUser() only when you need an up-to-date user record from the Auth server; it makes a network call. Use getSession() only when you need the raw access or refresh token. Reading a token is different from verifying the identity allowed to use server data. Keep the authorization check on the server with getClaims().

Every Server Action re-checks auth with getClaims(). An action is a public HTTP endpoint even if no button calls it. Validate mutation input, check claims, then write. Pages also check claims themselves. proxy.ts refreshes the session, copies cookies onto both the request and the response, and returns the response that setAll last built. Proxy is not the only lock.

For the Next.js 16 interceptor filename and cookie handling, see /fixes/nextjs-16-middleware-to-proxy. Browser code uses createBrowserClient; Server Components, Server Actions, and Route Handlers use createServerClient with cookies.getAll and cookies.setAll.

Last verified 2026-11-04. doc-verified; TypeScript snippets pass tsc --strict (TypeScript 5, @types/react 19) with zod and the Supabase client stubbed.

Pro

Next.js 16 + Supabase Pro rules, $1+

$1+ · pay what you want

RLS policy recipes, getClaims, Stripe webhooks, and Cache Components. Deeper than the free Next.js + Supabase pack.

Each pack ships as AGENTS.md, CLAUDE.md, Cursor .mdc, and Copilot instructions, with 12 months of free updates.

From the Next.js 16 + Supabase Pro rules

Enable RLS on every table in `public`. A table with no policy denies that command for `anon` and `authenticated`. A table with RLS off is readable and writable through the Data API. The free pack already says this. Write the policy, do not stop at `enable row level security`.
Name the role with `to authenticated`. Compare `(select auth.uid())` to `user_id`. The `select` wrapper is the form in the Supabase RLS guide. `auth.uid()` is null when the JWT is missing, and `null = user_id` is never true.
`select` and `delete` use `using`. `insert` uses `with check`, so a user cannot insert a row for someone else. `update` uses both: `using` picks the row, `with check` locks the row after the write, including `user_id`.
The secret key, and the legacy `service_role` key, bypass RLS. Use one only on the server, after you have checked the caller yourself. A browser client never gets that key. Supabase also says a secret key does not bypass RLS when the request still carries a user access token.
Buy Pro — $1+

Next.js + Supabase pack — Rules pinned to Next.js 16 and Supabase SSR — the APIs that actually work with React 19. Same advice in all four agent file formats, snippet-checked.

doc-verified; TypeScript snippets pass tsc --strict (TypeScript 5, @types/react 19) with zod and the Supabase client stubbed

Sources in the pack

Questions

Should Supabase SSR use getSession or getClaims on the server?
Use supabase.auth.getClaims() to protect server pages and data. Never trust getSession() for authorization: it reads the cookie without revalidating it, and anyone can forge that cookie.
Does getClaims always make a network call?
It verifies the JWT signature on every call, locally against cached JWKS for asymmetric signing keys, or through the Auth server for symmetric keys. It also refreshes the session when the access token is close to expiring.
When should I use getUser or getSession?
Use getUser() only for an up-to-date user record from the Auth server; it makes a network call. Use getSession() only for raw access/refresh tokens, never session.user for authorization. Every Server Action re-checks with getClaims().
When was this Supabase SSR rule last checked?
Last verified 2026-11-04. doc-verified; TypeScript snippets pass tsc --strict (TypeScript 5, @types/react 19) with zod and the Supabase client stubbed.