Supabase getSession vs getUser vs getClaims: which is safe on the server?
getSession(). Supabase says it reads the session out of the cookie without revalidating it, so its contents may not be authentic. Supabase's Next.js guide says to protect pages and user data with getClaims(), which verifies the token's signature, and to call getUser() when you need an up-to-date user record from the Auth server.The three methods, compared
| Method | What it does | Use it for |
|---|---|---|
getSession() | Reads the session from storage or cookies. Does not verify it. | Browser code that needs the current access token for the UI. Not for server-side decisions. |
getClaims() | Verifies the JWT signature. With asymmetric signing keys this is local against the project's public keys; with a symmetric secret it calls the Auth server. | Protecting pages, route handlers and server actions. Supabase's Next.js guide says "Always use supabase.auth.getClaims() to protect pages and user data." |
getUser() | Makes a request to Supabase Auth and returns the current user record. | When you need fresh data, for example whether the user still exists or their latest email. |
The vulnerable pattern and the fix
Vulnerable: an admin check built on an unverified cookie
// app/admin/page.tsx (Server Component)
const supabase = await createClient();
const { data: { session } } = await supabase.auth.getSession();
if (!session) redirect("/login");
const isAdmin = session.user.app_metadata?.role === "admin"; // not verified
Fixed: verify the token before you trust anything in it
const supabase = await createClient();
const { data, error } = await supabase.auth.getClaims();
if (error || !data?.claims) redirect("/login");
const userId = data.claims.sub;
const isAdmin = data.claims.app_metadata?.role === "admin";
Database queries made with the user's client are still filtered by row level security, because the database checks the token itself. The risk with getSession() on the server is in your own logic: redirects, admin checks, and anything you do with a secret key based on who the session says the user is.
What about Next.js middleware, now called Proxy?
Supabase's Next.js guide says: "Never trust supabase.auth.getSession() inside server code such as Proxy." The Proxy's job in that guide is to refresh the auth token by calling getClaims() and pass the refreshed cookies on. Follow the guide's createServerClient setup exactly, including returning the response object that setAll built, or the refreshed cookies never reach the browser.
Fix it, step by step
- Find every server-side call:
grep -rn "getSession()" app src lib middleware.ts proxy.ts - In each Server Component, route handler, server action and the proxy, replace
getSession()withgetClaims()and read the user ID fromclaims.sub. - Where you need the full, current user record, call
getUser()instead and accept the extra network request. - Keep
getSession()only in browser code, where it is the documented way to read the current token. - Never decide anything based on
user_metadata: users can edit their ownuser_metadata. Put roles inapp_metadataor a table that users cannot write.
Can a scanner catch this?
Caelo does not check auth logic, so it will not flag getSession() on the server. The grep above finds every call in a minute; the judgement of which ones run on the server is yours.
- Install Caelo: npx caelo . or the MCP server
- How Caelo compares with gitleaks, TruffleHog, Semgrep and others
1. Run the free local check
npx caelo .
Caelo is a free, open source (MIT) command line tool and MCP server from Irth Labs that checks the files of an AI-built app for exposed API keys, Supabase and Firebase misconfigurations and .env files with real values. It runs on your machine, uploads nothing and uses no AI model. Each finding comes with a plain-English fix.
2. Act on a finding
Follow the fix in your report and run Caelo again. If Git could commit an untracked file containing secrets, preview a US$19 quick fix for that file.
For findings that need an engineer, Irth Labs offers a US$490 App Health Check. You receive a written report within 48 hours and a fixed repair quote. The fee counts toward the repair if you proceed.
Questions
Why does getClaims() sometimes call the Auth server?
Supabase says that with asymmetric signing keys the JWT is verified locally against the project's JSON Web Key Set. With a symmetric JWT secret, getClaims() sends a request to the Auth server, like getUser().
Is getUser() slower than getClaims()?
Usually, because it always makes a network request to Supabase Auth. Use getClaims() for routine page protection and getUser() when you need the latest user record.
Why do I see a warning that using getSession() could be insecure?
supabase-js warns when the user object from getSession() is used on the server, because it came from storage without verification. Switching that code to getClaims() or getUser() is the fix.