We hand a login from one site to another in the URL fragment, and one anchor link makes the receiving function ignore it
Munchable has two front doors that are really two different apps. The marketing site and sign-up live on munchable.app, built with Next.js. The web version of the product lives on app.munchable.app, and it is the same Ex
Munchable has two front doors that are really two different apps. The marketing site and sign-up live on munchable.app, built with Next.js. The web version of the product lives on app.munchable.app, and it is the same Expo codebase that ships as the phone app, compiled for the browser.
Someone who has just made an account on the first should land in the second already signed in. A second login screen right after sign-up is the kind of dead end the product is supposed to avoid. The two hosts cannot share a cookie, so the session has to be carried across by hand.
The sending side is six lines
Here is the whole of lib/handoff.ts, minus comments:
export async function continueInBrowser(): Promise<boolean> {
const supabase = createClient();
const { data } = await supabase.auth.getSession();
const session = data.session;
if (!session?.access_token || !session?.refresh_token) return false;
const fragment = new URLSearchParams({
access_token: session.access_token,
refresh_token: session.refresh_token,
}).toString();
window.location.assign(`${WEB_APP_URL}/#${fragment}`);
return true;
}
The part that matters is the #. The tokens ride in the URL fragment, which a browser never sends to a server. The refresh token therefore stays out of request logs, out of the CDN, and out of anything that records a Referer. A query string would put it in all three. If you have ever pasted a magic link into a log search, you know why that is not a theoretical concern.
A visitor can see the handoff happen. The buttons on the home page that open the web version are the ones that do this when you are signed in, and when you are not, they send you to sign-up first. If you do create an account, the page you land on after it offers exactly this choice: continue in the browser, or get the phone app.
The receiving side is one parser for two producers
On the other end, authHandoff.ts in the Expo app has to accept more than our own handoff. It is the one consumer for two different producers:
- The handoff above:
#access_token=...&refresh_token=.... - An OAuth provider coming back: either the same implicit fragment, or the PKCE flow's
?code=...in the query string.
So there is one function that reads both places:
export function paramsFromUrl(url: string): URLSearchParams {
const hash = url.includes('#') ? url.split('#')[1] : '';
const query = url.includes('?') ? url.split('?')[1]?.split('#')[0] : '';
return new URLSearchParams(hash || query || '');
}
The fragment wins if it has anything in it, otherwise the query is used. On success the app calls history.replaceState to wipe the address bar, so the tokens do not sit in browser history or in a link someone copies. That cleanup is the other half of the fragment's promise, and it is why the page URL you see after arriving is just app.munchable.app.
Seven URLs
I pasted the parser into a scratch file and gave it seven URLs, asking whether each one "has a session" by the same test the app uses (an access_token or a code):
| URL (after the host) | Session found? |
|---|---|
/#access_token=a&refresh_token=b |
yes |
/?code=abc |
yes |
/?x=1#access_token=a&refresh_token=b |
yes |
/?code=abc#section |
no |
/#section?code=abc |
no |
/# |
no |
/?error_description=Access+denied |
no, but the message is read |
Six of the seven behave. The fourth does not, and it is the one that is easy to cause by accident. A PKCE return of ?code=abc that arrives with an anchor on the end is a perfectly valid URL, but because the fragment is non-empty it wins, the parser reads section as the parameter list, and the code in the query is never looked at. The function returns "nothing to consume", and a sign-in that succeeded at the provider looks like a failure in the app.
Nobody has hit this, because OAuth providers do not append anchors. That is exactly the kind of bug that is cheap to leave and expensive to explain later, and the fix is to read both places and prefer whichever actually contains a token:
const fromHash = new URLSearchParams(hash);
const fromQuery = new URLSearchParams(query);
return fromHash.get('access_token') || fromHash.get('code') ? fromHash : fromQuery;
One more asymmetry
The cleanup runs only when consuming the session succeeded. On a failure, the fragment, tokens and all, stays in the address bar. That is defensible, since a failed attempt may be worth reading, but it means the "tokens never linger" claim is true for successful logins only. I would probably strip the fragment either way, since a rejected token is no more useful to leave in history than an accepted one is.
I have not changed either of these yet. They are findings from reading the code and running it against awkward input, and the first is worth a test table next to the function: the seven rows above are the test.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.