Skip to content
FirstUser

For builders shipping this week

"Auth session missing!" or a login loop after deploy? 5 checks for Supabase apps

In the preview you log in and land on your dashboard. Your first real user logs in on the live site, sees the dashboard for a moment, and gets sent back to the login page. Or your console says "Auth session missing!". The password was right. Somewhere, the code that checks "who is this?" can't see the session. FirstUser opens your live URL like a brand-new visitor, signs up, logs in and tells you which step failed, with a screenshot and a fix prompt for Lovable, Bolt or Cursor. Run a free signup test.

"Auth session missing!" comes from supabase-js, not from your database. It is an AuthSessionMissingError with status 400. The library describes it as the error for an operation that requires a session when none is present. You usually see it from:

supabase-js exports isAuthSessionMissingError(error) so you can treat a missing session as logged out. For a visitor who is simply not logged in, this error is normal. It's a bug when it appears right after a successful login, or when your app bounces between /login and /dashboard.

This is not "Invalid login credentials" or "Email not confirmed". Those mean the login itself was refused. See "Email not confirmed" or "Invalid login credentials" right after signup. And if the user arrives from a confirmation or magic link and is logged out, start with email link opens your site, but the user isn't logged in.

1. Find out who is asking: the browser or the server

Open the live site in a private window, open DevTools, and log in.

  • The error shows up in the browser console, from your own component: the browser client has no session at that moment. Go to checks 4 and 5.
  • The error shows up in your server or function logs (Next.js Server Components, Route Handlers, middleware, API routes), or the redirect to /login comes back from the server before the page loads: the server can't see the session. Go to checks 2 and 3.

A quick tell: after login, look under Application → Local storage and Application → Cookies for your domain. A Supabase session in local storage and no sb-<project_ref>-auth-token cookie means server code that only reads cookies has nothing to use. That cookie name is the default from Supabase's SSR client guide.

2. Server code needs the session in cookies

Supabase's advanced SSR guide says the default behavior if you're not using SSR is to store the session in local storage, and that local storage isn't accessible by the server. For server-side rendering, the tokens have to live in a cookie, which is what @supabase/ssr does. By default, supabase-js stores the session in localStorage when the browser has it and you don't pass your own storage. persistSession defaults to true.

A likely cause of the loop is mixing those two setups:

  1. The login form uses a plain createClient() from @supabase/supabase-js. Login works, and the session lands in local storage.
  2. Middleware or a server page looks for a user, finds no auth cookie, and redirects to /login.
  3. The login page's client still sees the stored session and sends the browser to the dashboard.
  4. Back to step 2.

Fix: use createBrowserClient from @supabase/ssr in browser code and createServerClient with cookies.getAll and cookies.setAll in server code, as in the SSR client guide. Create the server client fresh for every request. Then log in again, because the old session only exists in local storage.

If your app is a pure client-side app (a typical Lovable or Bolt Vite app with no server rendering), this check doesn't apply. Skip to check 4.

3. The middleware (proxy) must refresh and pass the cookies on

Server Components can read cookies but can't write them, so Supabase's Next.js setup relies on a proxy that refreshes the session on every request by calling getClaims(). The SSR client guide lists three mistakes that sign users out:

  • Wrong file name. Next.js 16 renamed middleware.ts to proxy.ts, and the exported function is named proxy (Next.js proxy docs). middleware.ts is deprecated and still documented for the Edge runtime. Supabase notes that on Next.js 15 and earlier a proxy.ts file is never called, so sessions never refresh. There the file is middleware.ts with export async function middleware. The Supabase code inside is the same either way.
  • Wrong response. Return the supabaseResponse that setAll built last. Supabase: "An earlier response doesn't carry the refreshed cookies, so the user is signed out on the next request." If you need a redirect, copy the cookies onto it first.
  • Refreshing in two places. A refresh token can generally be used only once. Supabase allows a short reuse window, and it returns the active token when the parent of that token is presented. A reuse that matches neither exception revokes the whole session. Let the proxy do the refresh.

Also check the matcher. The guide's matcher skips routes that don't use Supabase. If yours also skips the routes that need the user, those routes never get a refreshed cookie.

Don't "fix" the error by switching the server check to getSession(). Supabase says never trust getSession() in server code such as the proxy, because it reads the cookie without revalidating it. Use getClaims() to protect pages. Use getUser() when you need the user record from the Auth server.

4. Don't redirect before the session has loaded

In the browser, supabase-js restores the saved session when the page starts. getUser() waits for that. A route guard often doesn't: it starts with user = null, decides "not logged in" on the first render, and redirects to /login. On the login page the session is now there, so it redirects back. That's the loop. Reload a protected page and you'll see it every time.

Fix: keep a loading state until you've heard from Supabase, and only then decide.

const [session, setSession] = useState<Session | null>(null)
const [ready, setReady] = useState(false)

useEffect(() => {
  const { data } = supabase.auth.onAuthStateChange((event, session) => {
    setSession(session)
    if (event === 'INITIAL_SESSION') setReady(true)
  })
  return () => data.subscription.unsubscribe()
}, [])

if (!ready) return <Spinner />
if (!session) return <Navigate to="/login" />

INITIAL_SESSION is the first event onAuthStateChange sends, after the stored session has loaded. The callback gets that session, or null. In Next.js, wait for the same event before you redirect.

The same timing problem breaks password resets: the reset page calls updateUser({ password }) before the session from the email link exists, and gets "Auth session missing!". Show the new-password form when onAuthStateChange reports PASSWORD_RECOVERY (the library's own example listens for that event), and make sure the link really creates a session. The email link guide covers that part.

5. Keep users on one address

The browser keeps a session per address. MDN says localStorage is a Storage object for the document's origin, and that "localStorage data is specific to the protocol of the document", so http:// and https:// of the same site get different storage. Cookies are tied to the host they were set for, too. A user who logs in on yourapp.com and is then sent to www.yourapp.com (or from your custom domain to the .lovable.app, Netlify or Vercel address) arrives logged out.

Typical places that switch addresses:

  • a redirectTo or emailRedirectTo built from a hardcoded URL or from the preview's window.location.origin
  • Stripe success_url / cancel_url pointing at the other variant (see logged out after checkout)
  • links in emails that use the Site URL while users browse a different domain

Fix: pick one address, redirect the other one to it at your host, and build every redirect from that one. Set the Supabase Site URL to it and put it on Redirect URLs. For Lovable apps on your own Supabase project, Lovable's docs say the Site URL is yours to set.

Test it like a stranger, after every deploy

Use a private window, your real live domain and a new account:

  1. Sign up and log in.
  2. Reload the page you land on.
  3. Paste a deep link (for example /settings) into a new tab.
  4. Log out, log in again.
  5. If you have a checkout, pay in test mode and check you come back logged in.

Each step should keep you logged in without a flash of the login page. If you'd rather not repeat that after every prompt, let FirstUser be the first user.

FAQ

Is "Auth session missing!" always a bug?

No. getUser() returns it for every visitor who isn't logged in. Treat it as "nobody is signed in" and show the logged-out view. supabase-js exports isAuthSessionMissingError(error) for that check. It's a bug when it shows up for someone who just logged in.

It works in the preview. Why does it only break after deploy?

The preview usually runs on one address, often with a dev server and a session you already have. The live site adds a new domain (and sometimes www), a production build, server rendering on the host, and visitors with empty browsers. Checks 2, 3 and 5 show up there.

Users get logged out at random, not right away.

Look at check 3 first. Supabase's SSR guide says refreshing the session in two places looks like users being signed out at random, because a refresh token can generally be used only once and a reuse outside the exceptions revokes the whole session. Also check the project's session settings: a time-box, an inactivity timeout, or single session per user. With single session per user, a sign-in on a phone ends the other session. supabase-js maps the Auth server's session_not_found code to this error.

Should I store the session somewhere else to make it stick?

Only if you render on the server, and then through @supabase/ssr cookies (check 2). For a client-only app, the default storage is fine. The fix is usually timing (check 4) or the address (check 5).

My app isn't built with Next.js.

The same rules hold. Supabase's SSR guide has cookie setups for SvelteKit, Astro, Remix, Nuxt, React Router, Express, Hono and TanStack Start. For Netlify-hosted Bolt apps, also check that deep links don't 404: see Bolt signup works in the preview but fails on Netlify. For a Cursor-built Next.js app on Auth.js instead of Supabase, see login works on localhost but not on Vercel.

Can FirstUser catch this?

FirstUser signs up and logs in on your live URL with a new email. It does not read your server logs or your Supabase dashboard. The login step does not reload the page and does not open a deep link in a new tab. If your page shows the error text in a visible role="alert" or data-error element, the login step is marked failed with "Login was rejected." and the diagnosis includes that text. If the page shows no signed-in state, the step fails with "Login was submitted, but the app did not show a signed-in state." Either way you get a screenshot of the step and a fix prompt. A loop that only appears when you reload, or when you open a protected page in a new tab, is still yours to check. Run a free signup test.

Checklist

Get this checklist by email

We will email this checklist to that address. Privacy policy

Sources