What it means
Every request carries the user's access token (a JWT) in the Authorization header. The token has an exp claim, and Supabase access tokens are short-lived on purpose (one hour by default). The session also holds a refresh token, which supabase-js uses to get a new access token before the old one runs out.
So “JWT expired” almost always means your code sent an old token itself instead of letting the client library refresh it. PostgREST v13 and later report it as PGRST303; older versions used PGRST301 with the same message.
Where stale tokens come from
- A copied token.
session.access_tokenwas saved in React state, localStorage or a custom header once, and reused hours later. - Server-side rendering without refresh. The server reads the session from a cookie, but nothing rotates it. With
@supabase/ssr, your middleware must call an auth method such assupabase.auth.getUser()on each request so expired cookies get refreshed and written back. - Mobile apps and sleeping tabs. Timers pause in the background, so the automatic refresh never fires and the first request after waking uses an expired token.
- Background jobs that stored a user token. Server jobs should use the secret key (or re-authenticate), not a user's access token captured earlier.
- A wrong device clock, which can also show up as “JWT issued at future”.
The fix in browser code
Let supabase-js attach the token. When you call your own API, ask for the session right before the call; getSession() refreshes it if it has expired:
// Supabase queries: the client attaches and refreshes the token for you
const { data, error } = await supabase.from('notes').select('*');
// Your own API: fetch the session right before the request, never cache the token
const { data: { session } } = await supabase.auth.getSession();
await fetch('/api/report', {
headers: { Authorization: `Bearer ${session?.access_token}` },
});The fix in React Native
Start and stop the refresh timer with the app state, as in Supabase's React Native guide:
import { AppState } from 'react-native';
AppState.addEventListener('change', (state) => {
if (state === 'active') supabase.auth.startAutoRefresh();
else supabase.auth.stopAutoRefresh();
});A safe retry for long-lived screens
async function withFreshToken(run) {
let res = await run();
if (res.error && ['PGRST303', 'PGRST301'].includes(res.error.code)) {
await supabase.auth.refreshSession();
res = await run();
}
return res;
}
const { data, error } = await withFreshToken(() => supabase.from('notes').select('*'));Retry once only. If the refresh itself fails, the refresh token is gone or revoked: sign the user out and send them to the login screen.
How to verify
Decode the token you are actually sending (from the browser's Network tab) and look at exp:
const payload = JSON.parse(atob(token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/')));
console.log('expires', new Date(payload.exp * 1000), 'now', new Date());If the expiry is in the past at the moment of the request, find where that token was stored and replace it with a fresh getSession() call.
What not to do
Don't raise the access token lifetime to days to make the error go away. A leaked token then stays valid for days, and signing a user out doesn't revoke tokens already issued.
Related guides
- Supabase Edge Function 401 “Invalid JWT” or “Missing authorization header”
- Supabase auth.uid() returns null – where it works, where it can't, and how to test policies
- Supabase service_role or secret key exposed in frontend code – detect, rotate, fix
- Supabase “Database error deleting user” – the foreign key on auth.users
More in Auth, JWT and secrets · all guides