Deutsch
Error messageSupabase · Auth

Fix “JWT expired” in Supabase without longer tokens or the service role key

Why Supabase answers JWT expired (PGRST303, HTTP 401), the four usual causes in AI-built apps, and the fix that keeps tokens short-lived and RLS in place.

The message you seeSupabase API (PostgREST) · PGRST303

JWT expired

By Tim GeithnerPublished 4 min read

Short answer

The request carried a Supabase access token whose expiry time has passed. Access tokens live for one hour by default, and supabase-js renews them on its own. You see this error when something bypasses that: auto refresh turned off, a token saved once and reused later, a token copied by hand into a script or tool, or a clock that is wrong. Let the Supabase client attach a fresh token to every request, and on the server read the token from each incoming request. Do not raise the JWT expiry to days and do not switch to the service role key.

Ask an AI about this page:ChatGPTClaude

On this page

Every request from a signed-in user carries an access token, a JWT with an expiry time inside. PostgREST, the API in front of your Supabase database, checks that time before it runs anything. If it has passed, the request is refused with HTTP 401, code PGRST303 and the message JWT expired. Nothing ran and nothing was saved. Access tokens live for one hour by default, and supabase-js renews them before that. So this error almost always means some code sent a token that supabase-js did not manage.

The response looks like this:

The error in the browser
{
  "code": "PGRST303",
  "details": null,
  "hint": null,
  "message": "JWT expired"
}

Find your cause in 30 seconds

When it happensMost likely cause
In the browser, after the app was open for about an hourAuto refresh is off or bypassed
In an Edge Function, API route, cron job or webhookServer code keeps an old token
In Postman, n8n, Zapier or a test scriptA token was copied by hand
Right after sign-in, or in the Supabase dashboardThe clock is wrong

The four causes

1. Auto refresh is off or bypassed

supabase-js renews the access token by itself because autoRefreshToken is on by default. It breaks when the app turns it off, or when code reads access_token once and puts it into its own fetch calls or a second client with a fixed Authorization header. That copy never gets renewed and stops working after an hour.

2. Server code keeps an old token

An Edge Function, API route or background job stored a user’s token: in a variable that lives across requests, in a database column, or in a queue message for later. When the job runs an hour later, the token has expired. A server should never keep access tokens. It should use the token that comes with each request.

3. A token was copied by hand

You copied a token from the browser’s network tab into Postman, an automation tool or a test script. It works for an hour, then fails with this message. That is the expiry doing its job.

4. The clock is wrong

The expiry is a timestamp, so the clocks matter. PostgREST allows 30 seconds of difference. If the computer that runs your code is minutes off, fresh tokens can look expired. Supabase’s troubleshooting guide names this as the usual cause when the dashboard itself shows JWT expired.

How to fix it

Let one Supabase client own the session and attach the token to every request. Do not read the token and carry it around yourself:

VulnerableBad: a token saved once and reused later
const { data } = await supabase.auth.getSession();
const token = data.session?.access_token;

// an hour later this request fails with "JWT expired"
await fetch(`${SUPABASE_URL}/rest/v1/notes`, {
  headers: { apikey: SUPABASE_ANON_KEY, Authorization: `Bearer ${token}` },
});
SafeGood: the client attaches a fresh token to each request
const { data: notes, error } = await supabase.from("notes").select("id, body");

const { data: summary } = await supabase.functions.invoke("summarize", {
  body: { noteId },
});

supabase.auth.onAuthStateChange((event) => {
  if (event === "SIGNED_OUT") window.location.assign("/login");
});

Both supabase.from() and supabase.functions.invoke() ask the client for the current session on every call, and the client renews it when needed. The listener sends the user to the login page when the session really ends, for example after a sign-out in another tab.

On the server, read the token from the incoming request every time, and build the Supabase client for that request only. In Edge Functions, use the pattern from Supabase’s guide to securing Edge Functions (opens in a new tab), which checks the caller’s token and gives you a client that still runs under row level security. For jobs that run later without a user, store what the job needs (the user id, the task), not the token. The Edge Functions security guide covers the details.

Fix JWT expired without weakening auth

My Supabase app fails with “JWT expired” (PGRST303 or PGRST301, HTTP 401). Find every place where the code reads session.access_token, builds its own Authorization header, creates a second Supabase client with a fixed token, sets autoRefreshToken to false, or stores a token in a variable, database column, cache or queue. Replace these with calls through the one Supabase client (supabase.from, supabase.functions.invoke), which attaches a fresh token on every request. On the server and in Edge Functions, read the token from each incoming request and never keep it. Add an onAuthStateChange listener that sends the user to the login page on SIGNED_OUT.

Do not increase the JWT expiry, do not use the service role key for requests made on behalf of a user and do not disable RLS. Show me the changes before applying them.

Check that the fix works

Find a failing request in the browser’s Network tab and copy the token after Bearer from its Authorization header. Paste it into this snippet and run it in the browser console. Do not paste tokens into websites:

Check: how long is this token still valid?
const accessToken = "paste the token here";
const [, payload] = accessToken.split(".");
const claims = JSON.parse(atob(payload.replace(/-/g, "+").replace(/_/g, "/")));
const secondsLeft = claims.exp - Math.floor(Date.now() / 1000);
console.log(secondsLeft > 0 ? `valid for ${secondsLeft} s` : `expired ${-secondsLeft} s ago`);

If it says expired, the request used an old token: find the code that sent it (causes 1 to 3). If it says valid but the API still answers JWT expired, check the clock of the machine that made the request (cause 4). After the fix, leave the app open for more than an hour, then save something. It must work without a new sign-in. The Supabase auth pitfalls guide lists the other session mistakes worth checking before launch.

Frequently asked questions

Should I set the JWT expiry to a week so users stop seeing it?

No. Supabase recommends the default of one hour and discourages values above that for security reasons. A stolen access token works until it expires, so a week-long token is a week-long key to the user’s data. Fix the code that sends old tokens instead; supabase-js renews tokens on its own.

Why PGRST303 and not PGRST301?

PostgREST 13 and later report an expired token as PGRST303, a failed claims check. PostgREST 12 and earlier used PGRST301 for the same message. Both come with HTTP 401 and the text JWT expired, and the fix is the same.

Can I just use the service role key on the server instead?

Not for requests made on behalf of a user. The service role key bypasses row level security, so every query sees every user’s rows. Your code would have to rebuild every check the policies did. Pass the user’s current token instead, so the database still knows who is asking.

The error shows up in the Supabase dashboard itself. What now?

Supabase’s troubleshooting guide says this usually means your computer’s clock is out of sync. Turn on automatic time in your system settings and reload the dashboard.

Sources

  1. 1.User sessions — Supabase Docs
  2. 2.PostgREST Error Codes — Supabase Docs
  3. 3.Authentication: JWT Claims Validation — PostgREST Documentation
  4. 4."JWT Expired" Error in Supabase Dashboard — Supabase Docs
  5. 5.Securing Edge Functions — 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