Supabase 'Invalid Refresh Token: Not Found' Logging Users Out
Users randomly signed out of your Supabase app with 'Invalid Refresh Token: Refresh Token Not Found'? Find the cause: duplicate clients, lost cookies, or session limits, and fix it.
The Supabase error AuthApiError: Invalid Refresh Token: Refresh Token Not Found means the browser sent a refresh token that Supabase no longer has. The usual causes are two Supabase clients fighting over one session, refreshed cookies that never reach the browser, or a session that Supabase ended on purpose. Check for duplicate createClient calls first, then check that your server code writes refreshed cookies back, then check your auth settings.
This bug is annoying because it hits a small share of users at random times. Everything looks fine in your own testing. Here is how to track it down.
Step 1: Know What the Error Means
A Supabase session has two tokens. The access token is a short JWT. The refresh token is a long-lived value stored in the Supabase database. When the access token expires, the client sends the refresh token and gets a new pair.
With refresh token rotation, each refresh token can be used once. After use, a new one replaces it. So "Refresh Token Not Found" means the token the client holds is not in the database. It was already replaced, or the session was removed.
Do not confuse it with "Refresh Token Already Used". That one points to two refreshes racing at the same moment. "Not Found" points to a stale or lost token. A write-up on the error makes the same distinction, and the GitHub issues for auth-js show how many people hit it.
Step 2: Find Where It Happens
Log the error with some context before changing anything. In the browser:
supabase.auth.onAuthStateChange((event, session) => {
console.log("auth event", event, !!session);
});
const { error } = await supabase.auth.getSession();
if (error) {
console.error("getSession failed", error.name, error.message, error.status);
}
Note which pattern you see:
- Always right after a deploy: your keys or project URL changed, or cookies are being dropped.
- Only in some browsers or devices: duplicate clients or storage problems.
- Only after a long idle period: session timeouts in your project settings.
- Only in the server logs, never in the browser: your server code is refreshing a session it cannot save.
Step 3: Make Sure You Have Only One Client
This is a top cause in apps made with AI builders such as Lovable or Bolt, where each generated file may create its own client. A Lovable guide on token expiry describes separate createClient() instances each holding their own session, so one refresh invalidates the token the others still hold.
Search your code:
grep -rn "createClient(" src/ | grep -v node_modules
You want one result in a shared file, imported 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
);
Also watch for this in hot reload and in server rendering. If you create a client inside a component body, a new one is made on every render. Create it once at module level, or memoize it.
Step 4: Check Server Code Writes Cookies Back
If you use server rendering (for example Next.js with @supabase/ssr), a server request may refresh the session. The new tokens must go into the response cookies. If they do not, the browser keeps the old refresh token, which Supabase already replaced. The next request fails with "Not Found". The auth-helpers issue about a repeat logout after refresh is an example of this class of problem.
The official pattern is a middleware that refreshes and writes cookies on both the request and the response:
import { createServerClient } from "@supabase/ssr";
import { NextResponse, type NextRequest } from "next/server";
export async function middleware(request: NextRequest) {
let response = NextResponse.next({ request });
const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
getAll: () => request.cookies.getAll(),
setAll: (cookiesToSet) => {
cookiesToSet.forEach(({ name, value }) =>
request.cookies.set(name, value)
);
response = NextResponse.next({ request });
cookiesToSet.forEach(({ name, value, options }) =>
response.cookies.set(name, value, options)
);
},
},
}
);
await supabase.auth.getUser();
return response;
}
Common mistakes here: returning a brand new NextResponse without copying the cookies, skipping the middleware for some routes, or calling getSession() on the server where getUser() should be used. Check the current @supabase/ssr docs for your version, since this API has changed over time.
Step 5: Clear Stale Cookies
Large sessions can be split into several cookie chunks. If an old chunk stays behind after a domain or key change, the client may read a mix of old and new data. A user who sees the error forever, while others are fine, may have a stale chunk.
Test in a private window first. If it works there, the problem is stored state. As a fix, sign the user out and clear the Supabase cookies and local storage keys that start with sb- on load when a session fails to refresh:
const { error } = await supabase.auth.getSession();
if (error?.message?.includes("Refresh Token")) {
await supabase.auth.signOut();
window.location.assign("/login");
}
This does not fix the cause. It gives users a clean way back in while you find it.
Step 6: Check Auth Settings and Rate Limits
In the Supabase dashboard, review the auth settings that can end sessions on purpose:
- Single session per user and time-box or inactivity timeout settings, where your plan supports them. Check the Supabase auth sessions docs for the exact options.
- Refresh token reuse interval, which gives a short window to reuse a token during a race.
- Rate limits on token refresh. A FlutterFlow thread describes an app that refreshed far too often until users hit a 429 and were logged out.
Then open the Auth logs in the dashboard and search around the time a user was logged out. The log shows the error code, so you can tell a reused token from a missing one.
Step 7: Handle Mobile and Offline Apps
Mobile apps lose network often. A refresh can start, the app goes to the background, and the response never arrives. Meanwhile the old token is already used. On return, the stored token is stale. A thread on offline-first React Native apps discusses this. Keep one client, let the library own refreshing, and avoid calling refreshSession() yourself in loops. If the user must sign in again after a long offline period, show a clear message.
For redirect problems after sign-in on a live domain, see Lovable Supabase login fails after publish. For access errors after login, see Supabase RLS errors in production.
How Relia Helps
Silent logouts are hard to spot because no one files a ticket. Relia watches users and APIs in a live product, and shows errors together with who was affected, so a pattern such as "many sign-ins failing after the last deploy" shows up as one incident in plain words. It supports Supabase and edge functions. It does not change your auth settings or code. You can see how this looks in the live demo.
FAQ
Why do my Supabase users get logged out randomly?
Most often the browser holds a refresh token that Supabase no longer has. Duplicate clients, server code that does not save refreshed cookies, and session limits in the dashboard are the main causes.
What is the difference between "Refresh Token Not Found" and "Already Used"?
"Already Used" means the token was used before, often by two refreshes at the same time. "Not Found" means the token is missing from the database, for example after a replaced or ended session.
Should I disable refresh token rotation?
No. Rotation protects against stolen tokens. Fix the client and cookie handling first. You can adjust the reuse interval to tolerate short races.
Does this error mean my Supabase project is down?
No. It is an auth error returned by a working service. A real outage would show network errors or 5xx responses instead.