Supabase "Invalid Refresh Token: Refresh Token Not Found": how to fix it
Your users get signed out at random, pages that need a login suddenly fail, and the browser console shows AuthApiError: Invalid Refresh Token: Refresh Token Not Found (or Already Used). The app worked in preview. Here's what's going on and how to fix it.
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 €49What the error means
When someone signs in, Supabase gives the browser two things: a short-lived access token and a refresh token. Before the access token expires, the Supabase client swaps the refresh token for a fresh pair. Each refresh token can only be used once. The error means the browser tried to use a refresh token that Supabase no longer accepts, because it was already used, the session was signed out or deleted, or it belongs to a different project.
1. Check you only create one Supabase client
This is the most common cause in AI-built apps. If createClient() is called inside a component, a hook or several files, you end up with several clients in the same tab. Each one tries to refresh the same session, the first one wins, and the others send a token that has just been used up.
Create the client once in its own file and import it everywhere:
// src/lib/supabase.ts
import { createClient } from '@supabase/supabase-js'
export const supabase = createClient(
import.meta.env.VITE_SUPABASE_URL,
import.meta.env.VITE_SUPABASE_ANON_KEY
)
Then search your code for createClient(. There should be one browser client (Next.js apps using @supabase/ssr also have a server client, which is fine).
2. Clear old sessions after changing project or keys
If you pointed the app at a new Supabase project, reset the database, or rotated keys, browsers still hold sessions from the old setup. Those refresh tokens will never work again. Signing out and back in fixes it for you, but real users will keep hitting it. Handle it in code so they're sent back to the login page cleanly:
supabase.auth.onAuthStateChange((event, session) => {
if (event === 'SIGNED_OUT' || (event === 'TOKEN_REFRESHED' && !session)) {
// send the user to your login page
window.location.assign('/login')
}
})
If the error still appears on load, catch it where you first read the session and call supabase.auth.signOut({ scope: 'local' }), which clears the stored session in that browser without another server call.
3. Next.js: refresh the session in middleware
In Next.js apps built with @supabase/ssr, sessions live in cookies. If middleware doesn't refresh the session and write the new cookies back on the response, the server and the browser can each refresh with the same token and one of them fails. Follow Supabase's server-side auth guide for Next.js: the middleware should create a server client, call supabase.auth.getUser(), and return the response that carries the updated cookies. Copy that file exactly; small changes to the cookie handling are a common source of this error.
4. Look for sign-outs you didn't mean
Calling signOut() without options signs the user out everywhere by default in current versions of supabase-js, which kills refresh tokens on their other devices and tabs. If your app calls it on errors or on page unload, those other sessions start failing. Use signOut({ scope: 'local' }) when you only mean this browser.
5. Check the session settings
In the Supabase dashboard open Authentication and look at the session settings. A very short time-box or inactivity timeout logs people out on purpose, and the next refresh fails with this error. Also check that Detect and revoke compromised refresh tokens isn't firing because of the duplicate clients from step 1.
Quick checklist
- One browser Supabase client, created once and imported everywhere
- Stale sessions sent back to login instead of a broken page
- Next.js middleware refreshes the session and returns its cookies
- No accidental global sign-outs
- Session timeouts set on purpose
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.