Dev.to Security 🔐 Cybersecurity 👁 0 📖 2 min read

Supabase PGRST301 "JWT expired": why it happens and how to fix it without weakening security

Your Supabase request fails with: {"code":"PGRST301","message":"JWT expired"} Users get kicked out, data stops loading, and it often shows up hours after login. Here is what it means and how to fix it without we

Your Supabase request fails with:

{"code":"PGRST301","message":"JWT expired"}

Users get kicked out, data stops loading, and it often shows up hours after login. Here is what it means and how to fix it without weakening your security.

What "JWT expired" means

When a user signs in, Supabase gives the browser an access token (a JWT). It is short-lived, one hour by default. Every API request sends it. When the token is past its expiry time, PostgREST rejects the request with PGRST301.

The access token is meant to expire. The client library is supposed to swap it for a fresh one using the refresh token. So this error almost always means the refresh step is not happening.

The 5 usual causes

  1. The session is not persisted or refreshed. A client created with persistSession: false or autoRefreshToken: false never renews.
  2. A stale token is cached on the server. Server code, edge functions or API routes copy the token once and reuse it.
  3. The tab was asleep. Browsers pause timers in background tabs, so a refresh is missed. supabase.auth.getSession() on return refreshes it.
  4. Server-side rendering without cookie refresh. In Next.js and similar frameworks, the token in the cookie must be refreshed in middleware, or server components read an expired one.
  5. Clock skew. If a device clock is wrong, a fresh token can look expired.

The fix, in order

  1. Check the client is created with defaults: autoRefreshToken: true, persistSession: true.
  2. Do not store the access token yourself. Ask the client for it each time: const { data } = await supabase.auth.getSession().
  3. For SSR frameworks, use the official helper package for your framework and refresh the session in middleware.
  4. Listen for onAuthStateChange and handle SIGNED_OUT and TOKEN_REFRESHED.
  5. If you must call the API from your own server, pass the user's current token per request, never a saved one.

Fixes that make it worse

  • Do not lengthen the JWT expiry to a week to hide the problem. A stolen token then works for a week.
  • Do not switch the client to the service_role key to stop the errors. That key bypasses row-level security and must never ship to a browser. If it is in your frontend bundle, rotate it now.

Check what your app ships

Because this error pushes people toward quick hacks, check afterwards that nothing sensitive got into the frontend. The free read-only check at https://proofforge.live looks for leaked keys and open data in a deployed site. For the question of whether one user can read another's rows, use the free two-account test: https://proofforge.live/two-account-test

Paid one-time fix reports are $9, $29 and $79, and the free scan does not require them.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.