Bare /join: use the viral-link token when the handler already consumed ?ref=

This commit is contained in:
martbost
2026-10-11 18:36:12 -05:00
parent b796a7a64e
commit 1287f94669
+2 -1
View File
@@ -1712,6 +1712,7 @@ const server = http.createServer(async (req, res) => {
if (/^[a-z0-9_]{1,20}$/.test(tok)) { try { known = !!((await accounts.byCode(tok)) || (await accounts.byUsername(tok)) || (/^\d+$/.test(tok) && (await accounts.byMemberId(Number(tok))))); } catch (e) {} }
const set = [];
if (known) {
req.viralTok = tok; // later routes (bare /join) can use the sponsor even before the cookie round-trips
const cookieTail = `; Path=/; SameSite=Lax; Max-Age=${30 * 24 * 3600}${IS_PROD ? '; Secure' : ''}`;
set.push('iap.sponsor=' + tok + cookieTail); // last touch wins, exactly like /join
set.push('iap.angle=page' + cookieTail); // the join shows under the "any page" hook
@@ -1749,7 +1750,7 @@ const server = http.createServer(async (req, res) => {
// the viral-link handler above, so the cookie decides; nobody known -> the company position, like the partner kit.
if ((p === '/join' || p === '/join/') && (req.method === 'GET' || req.method === 'HEAD')) {
const clean = s => String(s || '').toLowerCase().replace(/[^a-z0-9_]/g, '').slice(0, 20);
const refQ = clean(u.searchParams.get('ref')); const spon = (refQ && (JOIN_ALIASES[refQ] || refQ)) || clean(parseCookies(req)['iap.sponsor']); // ?ref= wins, then the cookie
const refQ = clean(u.searchParams.get('ref') || req.viralTok); const spon = (refQ && (JOIN_ALIASES[refQ] || refQ)) || clean(parseCookies(req)['iap.sponsor']); // ?ref= wins, then the cookie
u.searchParams.delete('ref'); const qs = u.searchParams.toString();
res.writeHead(302, baseHeaders({ Location: '/join/' + (spon || 'company') + (qs ? '?' + qs : ''), 'Cache-Control': 'no-store' })); return res.end();
}