Munchable has two front doors. The marketing site at munchable.app is Next.js. The web version of the app lives on its own subdomain and is the Expo app served through react-native-web. A visitor signs up on the first and, a few seconds later, wants to be using the second.
Signing in twice is the obvious failure. The less obvious part is how to avoid it without leaking a refresh token.
The shape of the problem
Sign-up happens on the marketing site with an email code. The moment it succeeds the visitor lands on a chooser page with two buttons: continue in the browser, or get the Android app. If they pick the browser, the session the marketing site just created has to arrive on the other origin. Cookies will not do it: the two hosts do not share a cookie scope by design, because the marketing site sets almost nothing and we want to keep it that way.
So the session has to travel in the navigation itself.
Why the fragment
Putting tokens in a URL is normally a bad idea. A query string is sent to the server, so it ends up in access logs, in proxies and in Referer headers. A fragment is different: the browser never sends it. Everything after the # stays on the client.
The sending side is 26 lines in total, and the whole mechanism is this:
const fragment = new URLSearchParams({ access_token: session.access_token, refresh_token: session.refresh_token, }).toString(); window.location.assign(`${WEB_APP_URL}/#${fragment}`); If there is no session to hand over, the function returns false and the caller sends the visitor to sign in rather than navigating to a page that would only fail. The chooser does the same when you reach it signed out: you are bounced to the sign-up page.
The consuming side
The receiving end is one function that two different producers share. The landing handoff is one. The other is a return from an OAuth provider, which can arrive as the same fragment or as a PKCE ?code= query. One consumer reads whichever is present:
if (accessToken && refreshToken) { await supabase.auth.setSession({ access_token, refresh_token }); } else if (code) { await supabase.auth.exchangeCodeForSession(code); } On success, and only on web, it rewrites the address bar:
window.history.replaceState(null, '', window.location.origin + window.location.pathname); That one line is why the refresh token does not sit in browser history or get copied when someone shares the link from the address bar.
Two quirks I found reading it back
Writing this up meant rereading the consumer, and two things stand out.
First, the parser prefers the fragment and falls back to the query: hash || query. If a URL ever carried both, the query would be ignored. Today no producer sends both, and the comment says so, but a function that silently picks one is the kind that surprises you in a year.
Second, the cleanup rewrites the URL to origin + pathname. That strips every query parameter, not just the auth ones. It is correct for our two producers. It would be wrong the day a deep link carried something the user cared about alongside a session.
Neither is a bug today. Both are the sort of thing a short test pins, which is a better use of ten minutes than a comment.
What is deliberately not here
There is no one-time code, no server-side handoff table and no signed redirect. A handoff table would be more conventional, and it would also be a new place that stores sessions. The fragment approach adds no storage at all, and the cost is that the receiving page must be one we control and must clear the address bar immediately, which it does.
Try it
The flow starts at munchable.app/get-started. After signing in you reach the chooser, and "Continue in browser" is the handoff described above. Watch the address bar as the web app loads: the fragment is there for a moment and then it is gone.