Fix a null Supabase magic-link session in TanStack Start

Trace the PKCE verifier, callback exchange, and session cookies when a Supabase magic link opens but your TanStack Start app stays signed out.

By Antoine RossPublished

If a Supabase magic link opens but your TanStack Start app still sees a null session, follow the request from the browser that asked for the link through the callback that exchanges its code. A successful email request alone does not create a session. PKCE is a sign-in flow that pairs a one-time code with a secret verifier created when the request begins. The callback needs that verifier to exchange the code for a session. Supabase's server-side guide explains why the browser and server need shared cookie storage.

This guide is for an existing app using Supabase's PKCE flow and a server-side callback. It assumes the app already has a working Supabase server client with request and response cookie handling. If you have not set that up, start with Supabase's TanStack Start quickstart and its server client. The checks below do not establish that email delivery, OAuth, or a hosted deployment works.

  1. Start and finish in the same browser. Request a new magic link from your app, then open it in the same browser profile. A different browser, private window, or device may not have the PKCE verifier. Request another link after each failed or completed attempt; do not reuse a consumed link.
  2. Check the sign-in request. Find the signInWithOtp call and its emailRedirectTo option. Confirm the response from your app reaches the browser with its cookie headers intact. If your client sends the request to an app endpoint, use a same-origin request and check its response in the browser's Network panel. A 200 response means the request succeeded; it does not prove that the callback created a session.
  3. Check the destination. Confirm that the redirect points to the callback route your app actually handles and that the URL is allowed in the correct Supabase project's URL configuration. Match the scheme, host, port, and path. Keep local and deployed origins separate. Do not paste the full email link or its code into a ticket, log, or chat.
  4. Check the callback exchange. The callback must read the code query parameter and call exchangeCodeForSession with a server client that can read the verifier cookie and write the resulting session cookies. Handle an exchange error as a failed sign-in; do not redirect to a protected page as though it succeeded.
  5. Check the next request. After the callback, reload a protected page. It should still show the test account. Sign out, then open that page again; it should require sign-in. If the callback succeeds but reload loses the session, inspect cookie scope and whether the response carries the cookies back to the browser.

In a reported TanStack Start question, the reader saw a null session after trying magic-link sign-in and later linked their own solution. A null session has several possible causes, so use the steps above to locate the failure in your app rather than assuming the verifier is always missing.

Inspect the handoff

Use a fresh test link and your browser's Network panel to follow the chain. Inspect whether each item is present and whether each operation succeeds; do not copy cookie values, full links, codes, or tokens into logs or support requests.

  • After requesting the link, check in browser cookie storage that the verifier cookie is present for the callback origin. If your app requests the link through a server endpoint, also check that its response includes the verifier Set-Cookie header. Check its domain, path, secure, and expiry attributes if it is missing on the next request.
  • On the callback request, check that the URL contains a code parameter and that the verifier cookie is sent. The code alone is not enough to complete PKCE.
  • In the callback's server-side handling, check whether exchangeCodeForSession returns an error. If it succeeds, check that the callback response includes the session Set-Cookie headers. Avoid logging their values.
  • On the next protected request, check that the session cookies are sent and that the server identifies the test account. If the exchange succeeded but this request is anonymous, inspect the cookie scope and any proxy or redirect that could have dropped the response headers.

For a CoreMVP TanStack Start project, the server client, email sign-in endpoint, and callback provide this cookie-backed path without building each part from scratch. The default sign-in form offers email and password; it does not show a magic-link button. To add a caller for the existing endpoint, follow Configure Password Authentication, then run the checks above for your own configuration.

Verify the boundary

Use a test account and a local or test inbox:

  1. Request a link through your same-origin app endpoint and open the email in the same browser profile. Reload a protected page; it should show the signed-in user.
  2. Sign out and request the protected page again. It should treat you as signed out.
  3. Open the same link a second time. Supabase should reject it, because a link works once.
  4. Request a new link in one browser profile and open it in a different profile. The callback has no matching verifier there, so it should return to sign-in instead of creating a session.

This checks the local flow, not hosted email delivery or provider configuration. For a real deployment, repeat the test with a test account and the deployed origin after configuring its redirect URLs.

If a fresh link fails before reaching your callback, inspect the destination, expiry, and email template first. If the callback receives a code but cannot exchange it, check the verifier and cookie handling. If exchange succeeds but the next request is signed out, inspect the returned session cookies and their scope. Supabase's PKCE flow guide describes the code exchange and its same-device requirement.