Deutsch
Security wikiAuth

What is a JWT (JSON Web Token)?

Also known as: JSON Web Token, access token, bearer token, session token

A JWT is a signed token that carries claims about a user. How Supabase uses JWTs for sessions and RLS, and which claims are safe to trust in a policy.

By Tim GeithnerPublished 2 min read

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:

  1. Decoding instead of verifying. An edge function splits the token, base64-decodes the payload and trusts sub. Anyone can write a token with any sub; only the signature check makes it trustworthy.
  2. Trusting user-editable claims. A signed-in user can change their own user_metadata (stored as raw_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-decode or split(".") 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 read user_metadata.
  • Read your admin policies. The safe and the unsafe version differ by one word:
VulnerableDon't: role from user_metadata
create policy "Admins read all reports"
  on public.reports for select
  to authenticated
  using ((select auth.jwt() -> 'user_metadata' ->> 'role') = 'admin');
SafeDo: role from app_metadata
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_jwt on for user-facing edge functions and read the user from a verified token, never from the request body.
  • Base access decisions on sub and app_metadata only.
  • 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. 1.RFC 7519: JSON Web Token (JWT) — IETF
  2. 2.JSON Web Token (JWT) — Supabase Docs
  3. 3.JWT Claims Reference — Supabase Docs
  4. 4.Row Level Security — Supabase Docs

Find this in your own app

ShipRely reads your repository (read-only), proves each finding with file and line, and gives you a fix prompt to paste into your AI builder.

Run a demo audit
  • Read-only access to your repository
  • Every finding with file and line
  • Fix prompts for Lovable, Cursor, Claude Code and Bolt