Supabase "JWT expired": how to fix it
Your app worked, then after about an hour (or overnight) every request fails with JWT expired. PostgREST usually answers HTTP 401 with code PGRST301 or PGRST303; Auth may say bad_jwt / "token is expired". The access token in the Authorization header is past its exp time. By default that token lasts one hour. supabase-js refreshes it automatically when it owns the session, so this error almost always means a token was copied out of the client and never refreshed.
Stuck on this? Get it fixed for €49.
Fixed in 48 hours or you don't pay. Launch deal for the first 5 customers (normally €79). You get the tested fix as a pull request, so nothing changes until you merge it.
Get it fixed for €491. Stop copying the access token into a global header
This is the most common Lovable / Bolt / Cursor pattern. Code like this looks fine at first, then dies after an hour:
// Bad: this header is never refreshed
createClient(url, key, {
global: { headers: { Authorization: `Bearer ${session.access_token}` } }
})
Create a normal client and let it attach the current session on each request:
const supabase = createClient(url, key)
await supabase.auth.signInWithPassword({ email, password })
// later: just call supabase.from(...).select() — no manual Bearer header
The same bug shows up when an Edge Function, webhook or fetch wrapper saves session.access_token into localStorage or a cookie and reuses it. Pass the Supabase client (or refresh on the server) instead of a stale string.
2. Refresh the session in Next.js middleware
Server Components cannot write cookies, so a refresh that only happens there is lost. Use @supabase/ssr with createServerClient, and refresh in middleware (or proxy.ts on newer Next.js) by calling supabase.auth.getUser() (or getClaims()) on the matched routes and returning the response that carries the updated cookies. Without that, a tab left open past the hour sends the old cookie JWT and gets "JWT expired". See also auth redirect loop on Vercel and Invalid Refresh Token.
3. Keep auto-refresh on, and handle a dead refresh
Leave autoRefreshToken at its default (true) on the browser client. If the refresh token itself is gone (signed out elsewhere, revoked, or the user cleared storage), do not keep retrying with the old access token. Sign the user out locally and send them to login:
const { data, error } = await supabase.auth.refreshSession()
if (error) {
await supabase.auth.signOut({ scope: 'local' })
// redirect to /login
}
If you see Auth session missing instead, the client you called has no session at all — different problem, same "send them to login" fix once you know why.
4. Don't lower JWT expiry too far
In the Supabase dashboard under Authentication → Sessions, a very short JWT expiry (a few minutes) makes every missed refresh visible as this error. Keep it at the default one hour unless you have a strong reason, and make sure your server refresh path runs more often than that.
5. Edge Functions and server routes
If a browser calls your Edge Function with a copied access token in a custom header, that token still expires. Prefer verifying the user inside the function with the Authorization header the client sends on each call (fresh from supabase-js), or create a server client with the user's cookies. Never embed a long-lived user JWT in your function secrets.
Quick checklist
- No
Authorization: Bearer ${access_token}baked into createClient or saved state - Browser client owns the session (auto refresh on)
- Next.js middleware refreshes cookies with
@supabase/ssr - Failed refresh sends the user to login
- JWT expiry left at a sensible default
Stuck on this? Get it fixed for €49.
Fixed in 48 hours or you don't pay. Launch deal for the first 5 customers (normally €79). You get the tested fix as a pull request, so nothing changes until you merge it.
Get it fixed for €49Still stuck?
Mendlark fixes this kind of problem for a fixed €49 (launch price, normally €79). We agree the problem first, test the fix and send it as a pull request, so nothing changes in your app until you merge it. If we can't fix it, you don't pay. Get the €49 fix, or start with a free scan.
Not sure what else is wrong? Paste your app link into the free instant check and get a plain-English list of problems in seconds.