Skip to content
>_VibeUnstuck

Auth session missing after refresh

Last verified:

The fix

Auth session missing: sign-in worked in the browser, but Next.js cannot see the Supabase cookie. Use the @supabase/ssr server client and refresh.

  1. 1

    Use the SSR helpers on the server

    In Next.js, create the server client with @supabase/ssr createServerClient, reading and writing the request cookies. Use createBrowserClient only in client components. Do not mix this with the older auth-helpers cookie format in the same app.

  2. 2

    Refresh the session before the page renders

    On Next.js 16, put that refresh in proxy.ts and export proxy. On an older Next.js, the same code lives in middleware.ts as middleware. Call getUser() so expired access tokens are refreshed, and copy setAll cookies onto the outgoing response. Exclude static files in the matcher or CSS and images get caught in auth logic.

    export const config = {
      matcher: [
        "/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
      ],
    };
  3. 3

    Exchange the code in the callback

    The route that receives ?code= must call exchangeCodeForSession with that code and then redirect. If this step is missing, the browser looks signed in for a moment and the next server render throws Auth session missing.

  4. 4

    Allow the site URL

    In Supabase, Authentication, URL configuration, set the site URL and add every redirect URL you use, including local ports and the deployed origin. A mismatch fails the code exchange even when the UI says the email link worked.

Why it happens

Supabase stores the session in cookies when you use @supabase/ssr on the server. A browser-only client keeps the session in memory or local storage, so a Server Component that reads cookies sees nothing after a refresh.

The cookie also goes stale unless proxy or middleware refreshes it by calling supabase.auth.getUser() and writing the cookies it returns onto the response. A callback route that never calls exchangeCodeForSession leaves you with a code in the URL and no session.

Redirect URLs are exact. http://localhost:3000 and http://127.0.0.1:3000 are different, and both must be listed if you use them.

The next error you'll probably hit

Once this is fixed, the usual follow-up is Invalid Refresh Token: Refresh Token Not Found. Supabase rejected the refresh token in the cookie. It was already rotated or revoked, or the new token was never written back onto the response.

Prompt to paste back into your AI tool

Paste this above the error.

Supabase reports Auth session missing after refresh. The user can sign in and then the server does not see the session.

Do not store the session only in React state. Do not switch to the service role key to skip auth.

1. Use @supabase/ssr createServerClient for server code and createBrowserClient for client code.
2. Refresh the session in proxy.ts (Next.js 16, export proxy) or middleware.ts (older Next), calling getUser() and writing cookies onto the response. Exclude _next/static from the matcher.
3. Make the auth callback call exchangeCodeForSession.
4. Tell me the exact redirect URLs to add in the Supabase dashboard.
5. Do not rewrite the rest of the app.

Common questions

+getSession() says there is a session and the page still misses it. Why?

getSession() reads the local cookie without guaranteeing a refresh. On the server, getUser() validates the token and is the call that should run in proxy or middleware so cookies are rotated.

+Why did this start after an AI added middleware?

The new file probably redirects before the Supabase client can write cookies, or it replaced the response and dropped Set-Cookie. The response returned at the end has to be the one that received setAll.

Related errors