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
Use the SSR helpers on the server
In Next.js, create the server client with
@supabase/ssrcreateServerClient, reading and writing the request cookies. UsecreateBrowserClientonly in client components. Do not mix this with the olderauth-helperscookie format in the same app. - 2
Refresh the session before the page renders
On Next.js 16, put that refresh in
proxy.tsand exportproxy. On an older Next.js, the same code lives inmiddleware.tsasmiddleware. CallgetUser()so expired access tokens are refreshed, and copysetAllcookies 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
Exchange the code in the callback
The route that receives
?code=must callexchangeCodeForSessionwith that code and then redirect. If this step is missing, the browser looks signed in for a moment and the next server render throwsAuth session missing. - 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
- Invalid Refresh Token: Refresh Token Not FoundSupabase rejected the refresh token in the cookie. It was already rotated or revoked, or the new token was never written back onto the response.CursorLovableBoltSupabaseAuthOct 23, 2026
- The "middleware" file convention is deprecated. Please use "proxy" instead.Next.js 16 still runs middleware.ts and prints this warning. The file is now proxy.ts, the export is proxy, and the runtime is Node.js.Cursorv0BoltNext.jsAuthOct 23, 2026
- ERR_TOO_MANY_REDIRECTS on loginERR_TOO_MANY_REDIRECTS on login means the guard and the login page redirect to each other. Exclude the sign-in route and read the session from one place.Cursorv0BoltAuthNext.jsOct 14, 2026
- Error 400: redirect_uri_mismatchGoogle rejected the OAuth request because the redirect_uri it received is not one of the Authorized redirect URIs on that OAuth client. The deployed domain and the callback URL are different strings.CursorLovableBoltAuthSupabaseNov 11, 2026
- Email not confirmedSupabase Auth refused sign-in because this user has not confirmed their email. The error code is email_not_confirmed.CursorLovableBoltSupabaseAuthOct 27, 2026
- new row violates row-level security policynew row violates row-level security policy: RLS is on and no INSERT policy allows this role. Add a policy for the signed-in user. A missing GRANT differs.CursorLovableBoltSupabasePostgresOct 15, 2026
- email rate limit exceededemail rate limit exceeded: Supabase Auth refused another signup or reset. The built-in mailer is capped. Wait, or set custom SMTP. Resending will not help.CursorLovableBoltSupabaseAuthOct 16, 2026
- Invalid API keyInvalid API key. Supabase received a key this project does not accept. Copy the publishable key, or the legacy anon key, from the same project as the URL. No API key found in request means the header was missing.CursorLovableBoltSupabaseNext.jsNov 6, 2026