Fix Supabase "Auth session missing!" on page load and on the reset-password page
DEV Community

Fix Supabase "Auth session missing!" on page load and on the reset-password page

Your app loads, or a user clicks the link in a password reset email, and the console fills up with one of these:

Auth session missing!
AuthSessionMissingError
AuthSessionMissingError: Auth session missing!

The short version: Supabase Auth looked for a signed-in session and found none. Something called getUser() or updateUser() before a session existed, or somewhere that can't see it.

I see it often in apps built with Lovable, Bolt or Replit on Supabase, and almost always in one of two places: the first render of the app, and the change-password page.

It isn't a server error. In the supabase-js source, AuthSessionMissingError is a CustomAuthError with the message "Auth session missing!" and status 400. Supabase's Auth error docs describe CustomAuthError as an error that "generally originate[s] from state in the client library", and say to identify it by its name property. In other words, the client is telling you about its own state: there's no session here.

I wrote about this error once before, together with "Invalid Refresh Token", from the angle of sessions that disappear after refreshes, tabs and sign-outs. This post is about the other half: code that asks for the user too early, the password reset flow, and server and Edge Function code that never gets the session.

Why it happens

  • The code runs before there's a session. A component calls getUser() on page load, before supabase-js has a session to work with, or the user simply isn't signed in. The getUser() reference says it "gets the current user details if there is an existing session". No session, no user, and you get AuthSessionMissingError instead.
  • The reset page updates the password too early. updateUser() "updates user data for a logged in user". On a change-password page, that session only exists after the recovery link has been handled: with the implicit flow it arrives with the PASSWORD_RECOVERY event, and with the PKCE flow only after the link's token is exchanged.
  • A PKCE ?code= link was opened in another browser, or too late. Supabase's PKCE docs say the code verifier is "created and stored locally when the Auth flow is first initiated", so the exchange "must be initiated on the same browser and device where the flow was started". The code is valid for 5 minutes and can be exchanged only once.
  • A server-side client can't see the cookies. Supabase's server-side guide says SSR "requires cookie-based session storage", and that on the server you configure the client anew for every request, "because you need the cookies from the request". A client created without them has no session.
  • An Edge Function never received the user's token. When a signed-in user calls a function, the request carries their session JWT in the Authorization header. A client created in the function without it, or a plain fetch with no header, has no session.

Find the cause

Find the line that throws. It's almost always supabase.auth.getUser() or supabase.auth.updateUser().

In browser code, log supabase.auth.getSession() just before it. The reference says that session "can be null" when the user "is not signed-in or has logged out". Null here means the code ran too early or nobody is signed in. Don't use this trick as a security check on the server: Supabase says never to trust getSession() in server code, because it reads the cookie without revalidating it.

On a reset page, look at the address the email link opens. #access_token=... in the fragment is the implicit flow. ?code=... is the PKCE flow, and so is a token_hash link to your own /auth/confirm route.

If it's a ?code= link, open it in the same browser that requested it, within 5 minutes. If that works, the code verifier was the problem.

For server code and Edge Functions, check that the request's cookies or Authorization header actually reach the Supabase client.

Fix 1: wait for the session before asking for the user

Subscribe to onAuthStateChange once, when the app starts. The events it reports include INITIAL_SESSION, SIGNED_IN and SIGNED_OUT. When there's no session, show the sign-in screen instead of calling getUser(), and only load user data once a session exists:

const { data } = supabase.auth.onAuthStateChange((event, session) => {
  if (!session) {
    showSignIn() // signed out: do not call getUser() here
    return
  }
  loadAccount(session.user.id)
})
// on unmount
data.subscription.unsubscribe()

That's the pattern from our fix page, and it matches the React example in the onAuthStateChange reference, which keeps the session in state and clears it on SIGNED_OUT.

Fix 2: fix the reset-password page

Supabase's password guide splits password reset by flow. If you're using SSR, you're using PKCE. In client-only JavaScript the default is the implicit flow.

  • Implicit flow: the reset email links to your change-password page. Wait for the PASSWORD_RECOVERY event there, then call updateUser with the new password.
  • PKCE flow: change the reset email template so the link goes to {{ .SiteURL }}/auth/confirm?token_hash={{ .TokenHash }}&type=recovery, verify it on that route with verifyOtp, and only then show the form and call updateUser.
// Implicit flow: the recovery link signs the user in, then fires PASSWORD_RECOVERY
supabase.auth.onAuthStateChange(async (event) => {
  if (event === 'PASSWORD_RECOVERY') {
    const password = await askForNewPassword()
    const { error } = await supabase.auth.updateUser({ password })
    if (error) console.error(error.message)
  }
})

// PKCE flow (/auth/confirm?token_hash=...&type=recovery): exchange first
const { error } = await supabase.auth.verifyOtp({ type: 'recovery', token_hash })
// then show the form and call supabase.auth.updateUser({ password })

Two more things from the docs. The change-password page's address has to be in your redirect URLs, under Authentication > URL Configuration. And the Site URL there, which defaults to http://localhost:3000, is the default redirect for password resets, so change it to your production URL.

Fix 3: pass the session on the server and in Edge Functions

Server-rendered apps: create the Supabase client with the request's cookies, using the @supabase/ssr setup from the Server-Side Auth guide for your framework. To protect pages and user data on the server, the guide recommends supabase.auth.getClaims(), which verifies the token, rather than getSession().

Edge Functions: call them with supabase.functions.invoke from a signed-in client, so the user's token travels in the Authorization header, and read the user from that header inside the function. The current Securing Edge Functions guide does this with the withSupabase wrapper and auth: 'user'.

// From a signed-in client: the user's session JWT goes in the Authorization header
const { data, error } = await supabase.functions.invoke('hello', {
  body: { foo: 'bar' },
})

One trap from the Authorization headers guide: don't send the publishable key as a bearer token. The new API keys aren't JWTs. Put them in the apikey header and keep Authorization for user tokens.

Check the fix worked

Load the app signed out. It shows the sign-in screen, and the console has no AuthSessionMissingError.

Request a password reset, open the link in the same browser, set a new password and sign in with it. If you use ?code= links, open one in a different browser. The app should show a clear "request a new link" message, not a raw error.

Stop it coming back

Gate every signed-in page on the session from onAuthStateChange, not on a getUser() call at load. Test the whole password reset on the live domain, on desktop and on a phone, before launch. Tell users on the reset page to open the link in the same browser, within a few minutes.

Sources: Supabase: Password-based auth, PKCE flow, Implicit flow, Auth error codes, Server-Side Auth: creating a client, Securing Edge Functions, Authorization headers, Redirect URLs, JS reference: onAuthStateChange, getUser, getSession, updateUser, functions.invoke, supabase-js errors.ts.

The full checklist, with related Supabase sign-in fixes, is on our fix page. If sign-in, password reset or sessions still break for some users, the problem is usually in how the app handles the session; our free AI-built app audit reads the code and lists what's broken, with the file and line. I wrote this with AI assistance and reviewed it myself.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.