Short answer
A JSON Web Token (JWT) is a compact, signed string that carries claims, such as who the user is and when the token expires. Supabase Auth issues one per session, and Postgres reads its claims through auth.uid() and auth.jwt() in RLS policies. Trust a JWT only after its signature and expiry are verified, and base access decisions on claims the user cannot edit, such as app_metadata.
Ask an AI about this page:ChatGPTClaude
On this page
A JSON Web Token (JWT) is a compact, URL-safe string that carries claims, signed so the receiver can tell whether anyone changed them. RFC 7519 defines it. A JWT has three parts separated by dots: a header (which algorithm signed it), a payload with the claims, and the signature. Standard claims include sub (the subject, usually the user ID) and exp (the time after which the token must be rejected).
JWTs in an AI-built app
In a Supabase app, Supabase Auth issues a JWT, the access token, when a user signs in and keeps issuing fresh ones as long as the session lasts. The supabase-js client sends it with every request. Postgres then reads it inside your RLS policies: auth.uid() returns the sub claim and auth.jwt() returns the whole payload. Supabase’s claims include sub, role, aal, exp, email, and the optional app_metadata and user_metadata.
Two mistakes show up again and again in generated code:
- Decoding instead of verifying. An edge function splits the token, base64-decodes the payload and trusts
sub. Anyone can write a token with anysub; only the signature check makes it trustworthy. - Trusting user-editable claims. A signed-in user can change their own
user_metadata(stored asraw_user_meta_data), and the next JWT carries the change. A policy that reads a role from there lets anyone make themselves admin.
How to check your app
- Search your edge functions for
atob(,jwt-decodeorsplit(".")near the Authorization header. Each must be paired with a real verification call, or replaced by one. - In the Security Advisor, lint 0015 (
rls_references_user_metadata) flags policies that readuser_metadata. - Read your admin policies. The safe and the unsafe version differ by one word:
create policy "Admins read all reports"
on public.reports for select
to authenticated
using ((select auth.jwt() -> 'user_metadata' ->> 'role') = 'admin');create policy "Admins read all reports"
on public.reports for select
to authenticated
using ((select auth.jwt() -> 'app_metadata' ->> 'role') = 'admin');app_metadata can’t be changed by the user; set it from server code or keep roles in a table with its own RLS.
How to prevent problems
- Verify before you trust: keep
verify_jwton for user-facing edge functions and read the user from a verified token, never from the request body. - Base access decisions on
subandapp_metadataonly. - Treat the payload as public. It is readable by anyone holding the token.
- Read more about sessions, redirects and confirmation in Supabase Auth pitfalls, and about a function that trusts the caller’s ID in Edge function trusts user_id from the body.
Frequently asked questions
Is a JWT encrypted?
Usually not. A normal JWT is signed, not encrypted: anyone who has it can decode and read the claims. The signature only proves that nobody changed them. Never put secrets in a JWT.
Can a user change the claims in their Supabase JWT?
Not directly, because that breaks the signature. But a signed-in user can change their own user_metadata, and the next token then carries the new value. That is why policies must not trust user_metadata.
Should my edge function decode the JWT to get the user ID?
Decoding is not verifying. Use a function that checks the signature and expiry, such as supabase.auth.getClaims(), or keep verify_jwt on, before you trust the sub claim.
Sources
- 1.RFC 7519: JSON Web Token (JWT) — IETF
- 2.JSON Web Token (JWT) — Supabase Docs
- 3.JWT Claims Reference — Supabase Docs
- 4.Row Level Security — Supabase Docs