caelum0x 6066b4fca6 fix(console): /auth/start no longer dead-ends marketing CTAs with a raw 400
The Insights cross-sell banner, the Rithub landing page, and the
Relay-vs-OpenRouter comparison page all link straight to
`/auth/start?src=<slug>` with no `provider`/`email` for a logged-out
visitor (the normal case). That hit the fallback branch, which returned
a developer-facing `400 Pass ?provider=google|github or
?email=you@company.com` instead of taking the visitor anywhere — every
"Start free" / "Open Railyard Console" CTA on those pages 400'd for any
visitor without a known email, independent of the separate
Scribase-project outage already tracked in plans/railyard.md.

Per the GTM campaign-workflow playbook's "ad claims vs current product
page" / "follow a test submission end to end" checks
(plans/gtm/_archive/CAMPAIGN-WORKFLOW-90D-2026-10-10.md), redirect to
the real sign-in page instead, preserving the `src` attribution cookie
the same way the provider/email branches already do — `auth/callback`
and `lib/email-signin.ts` read that same cookie at completion time, so
attribution is unaffected.

Verified live: built with real required env vars (prerender-clean, all
4 OIDC routes included), ran `next start`, and curled the exact CTA
URLs used by the three pages — pre-fix returns 400, post-fix returns
302 to the sign-in page with `ry_src` set correctly.
2026-11-05 00:01:42 +03:00

69 lines
3.2 KiB
TypeScript

import { deps } from "../../../lib/deps.ts";
import { cookie } from "../../../lib/http.ts";
import {
EMAIL_COOKIE,
EMAIL_COOKIE_TTL_SECONDS,
normalizeEmail,
PKCE_COOKIE,
PROVIDERS,
sendEmailCode,
startSignIn,
type Provider,
} from "../../../lib/scribase-auth.ts";
import { normalizeSrc, SRC_COOKIE, SRC_COOKIE_TTL_SECONDS } from "../../../lib/session.ts";
export const dynamic = "force-dynamic";
/**
* One entry point for every "come sign up" deep link, including the
* cross-sell bridge from other Railyard products (console cross-sell
* bridge, RYP-X1): `/auth/start?provider=google`, or
* `/auth/start?email=jane@acme.com&src=insights` to prefill and immediately
* send an email sign-in code, carrying `src` through to the new org's
* `signup_source` (see lib/session.ts).
*/
export async function GET(req: Request) {
const { cfg } = deps();
const url = new URL(req.url);
const provider = url.searchParams.get("provider") as Provider;
const email = normalizeEmail(url.searchParams.get("email"));
const src = normalizeSrc(url.searchParams.get("src"));
const srcCookie = src ? [cookie(SRC_COOKIE, src, SRC_COOKIE_TTL_SECONDS)] : [];
if (PROVIDERS.includes(provider)) {
const { url: authorizeUrl, verifier } = await startSignIn({ scribaseUrl: cfg.scribaseUrl, consoleUrl: cfg.consoleUrl, provider });
const headers = new Headers({ location: authorizeUrl });
headers.append("set-cookie", cookie(PKCE_COOKIE, verifier, 600));
for (const c of srcCookie) headers.append("set-cookie", c);
return new Response(null, { status: 302, headers });
}
if (email) {
try {
await sendEmailCode({ scribaseUrl: cfg.scribaseUrl, anonKey: cfg.scribaseAnonKey, email });
} catch (err) {
console.error(`cross-sell sign-in: ${(err as Error).message}`);
return new Response(null, { status: 302, headers: { location: `${cfg.consoleUrl}/?signin=failed` } });
}
const headers = new Headers({ location: `${cfg.consoleUrl}/?step=code` });
headers.append("set-cookie", cookie(EMAIL_COOKIE, encodeURIComponent(email), EMAIL_COOKIE_TTL_SECONDS));
for (const c of srcCookie) headers.append("set-cookie", c);
return new Response(null, { status: 302, headers });
}
// Neither a recognized provider nor an email was supplied -- this is the
// common case for the Insights cross-sell banner (RYP-X1) and the public
// comparison/Rithub landing pages, which link here with only `?src=<slug>`
// when the visitor has no known email yet. Previously this fell through to
// a raw 400 with a developer-facing message, which meant every one of
// those marketing CTAs 404/400'd end-to-end for a logged-out visitor
// instead of landing them on the real sign-in page -- found auditing those
// pages' "Start free" CTA against actual behavior. Preserve the `src`
// attribution cookie and send the visitor to the normal sign-in page,
// where they can pick a provider or type their email; `auth/callback` and
// `lib/email-signin.ts` already read this same cookie at completion time.
const headers = new Headers({ location: `${cfg.consoleUrl}/` });
for (const c of srcCookie) headers.append("set-cookie", c);
return new Response(null, { status: 302, headers });
}