Supabase AuthApiError: Invalid Refresh Token: Refresh Token Not Found

By AI App Medic · Updated · Supabase · Auth · v0 · Lovable

A user who was signed in yesterday opens your Lovable, Bolt or v0 app and lands on the login screen. The console or server log shows:

AuthApiError: Invalid Refresh Token: Refresh Token Not Found

Sometimes it happens after they signed out on another device, after you shipped a Next.js or v0 change, or after you switched Supabase projects. It can look random because the access token still works for up to an hour. The failure appears only when the client tries to renew the session.

What that message actually means

A Supabase session is a short-lived access token plus a refresh token. The refresh token can be used once. When the client presents it, Auth issues a new pair and invalidates the old refresh token. There is a short reuse window (10 seconds on hosted Supabase) so a legitimate double submit does not kill the session. Outside that window, a reused or missing token is treated as a problem, and this error is the result.

Nearby messages are easy to mix up. Match the text before you change code:

MessageWhat happened
Refresh Token Not FoundAuth has no record of this token. It was revoked, already traded in a while ago, or it belongs to a session that was deleted.
Already UsedThe same token was presented again after the reuse window. Auth ends the whole session, which is why the user is signed out rather than just retried.
Session Expired, sometimes with (Inactivity) or (Revoked by Newer Login)A limit you set under Authentication, Sessions (time-box, inactivity timeout, or a single session per user) ended the session on purpose.

Cause 1: signOut() ended every device

With no options, supabase.auth.signOut() revokes every session that user has: laptop, phone and other browsers. The other devices still hold an old refresh token. The next renewal throws Refresh Token Not Found, often an hour later, and it feels like the app signed them out for no reason.

If each device should stay signed in until that device signs out, be explicit:

await supabase.auth.signOut({ scope: "local" });

Search the project for every signOut call, including ones a builder added on a settings page, and change them unless you really want a global logout. After a global sign-out, the other devices cannot recover the old session; the user has to sign in again, which is expected.

Cause 2: a stale token in the browser (Lovable and Bolt apps)

Most Lovable and Bolt apps keep the session in localStorage on a single browser client. That is fine until the stored token no longer exists on the server:

Create one shared browser client and reuse it. Do not disable session persistence unless you have a reason. When you change projects, bump a storage key or document that users need to sign in again. In the UI, treat this error as "the session is gone": sign out locally and show the login screen, rather than leaving a spinner or a crash.

import { createClient } from "@supabase/supabase-js";

export const supabase = createClient(
  import.meta.env.VITE_SUPABASE_URL,
  import.meta.env.VITE_SUPABASE_ANON_KEY,
);

export async function getCurrentUser() {
  const { data: { session }, error } = await supabase.auth.getSession();
  if (error || !session) return null;
  const { data: { user } } = await supabase.auth.getUser();
  return user ?? null;
}

Call getSession first. getUser is the call that throws when nothing is stored. A signed-out visitor is a normal state, not an exception.

Cause 3: two refreshes at once (v0 and Next.js)

On a server-rendered app the browser and the server both renew the session. The token is single use, so one side wins and the other can see Already Used or, later, Not Found. A known bug in older @supabase/ssr versions also started a background refresh when the server client was constructed, which raced your own getUser() call. That was fixed in @supabase/ssr@0.8.1. Upgrade first:

npm install @supabase/ssr@latest @supabase/supabase-js@latest

Then follow three rules from the current SSR setup:

  1. Refresh in one place per request, usually middleware (or the Next.js 16 proxy file, if that is what your app actually uses). Do not also call getUser() or getSession() in a layout on the same request if middleware already did.
  2. Build a new server client inside each request. Do not cache one at module scope. On some hosts a shared client can be reused across visitors.
  3. Return the same response object that your cookie setAll adapter wrote to. Creating a fresh NextResponse.redirect or NextResponse.next afterwards, without copying those cookies, leaves the browser with the old refresh token. The next request then presents a token the server has already used.
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() {
          return 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);
          });
        },
      },
    },
  );

  // Do not skip this call: it is what refreshes the session.
  await supabase.auth.getUser();
  return response;
}

A setAll that is a no-op (empty body, added to silence a type error) guarantees this bug: the server refreshes, the browser never learns the new token, and the next hit fails. If you redirect after the check, copy the cookies onto that redirect response instead of returning a brand new one.

Cause 4: session limits you turned on

Under Authentication, then Sessions, a Pro project can set a maximum session length, an inactivity timeout, or a single session per user. Those end sessions on purpose. The first failure is usually Session Expired. About a day later the deleted session can surface as Not Found instead. If the log includes (Revoked by Newer Login), a sign-in on a second device is doing what you configured. Turn the limit off, or tell users what to expect, rather than chasing a client bug.

Prevention

Use a local sign-out by default, one shared browser client, and a current @supabase/ssr on any server-rendered app. After you rotate a project, delete users or change Auth settings, expect existing browsers to need a fresh login. When the error does appear, catch it, clear the local session and send the user to sign in. Do not leave it unhandled; that is what turns a routine renewal into a broken page. If the user never gets a session in the first place, that is a different path: start with login redirects to localhost or, for Google's Error 400, redirect_uri_mismatch.

FAQ

Why does signing out on my phone also sign me out of my laptop?

supabase.auth.signOut() with no options uses a global scope. That revokes every refresh token the user has. The other device fails with Refresh Token Not Found the next time it tries to renew the session. Use scope: 'local' to end only the current session.

The error happens in Next.js middleware, not in the browser. What then?

Two parts of the app are probably refreshing the same single-use token, or the new cookies are not written back to the response. Upgrade @supabase/ssr to at least 0.8.1, call getUser in one place per request, and always return the response object the cookie adapter mutated.

Is this a Google login misconfiguration?

No. A bad Google redirect URI fails before a session exists. This error means a session existed and its refresh token is no longer valid. Fix sign-out, cookie handling and stale storage first.

Related guides