From 9acdce4a25d99b0e3441117ca81cb4c7a19f2198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Fri, 6 Nov 2026 20:27:17 +0000 Subject: [PATCH] feat(cli): the sign-in page the browser lands on follows the design (#7332) * feat(cli): the sign-in page the browser lands on follows the design * style(cli): format the sign-in page files * fix(cli): the sign-in page picks Cancel or failure inside the page builder, and drops unused code --- packages/cli/src/auth/callbackPage.ts | 80 ++++++++++++++++++++++++++ packages/cli/src/auth/loopback.test.ts | 25 +++++++- packages/cli/src/auth/loopback.ts | 32 ++--------- 3 files changed, 109 insertions(+), 28 deletions(-) create mode 100644 packages/cli/src/auth/callbackPage.ts diff --git a/packages/cli/src/auth/callbackPage.ts b/packages/cli/src/auth/callbackPage.ts new file mode 100644 index 0000000000..67f5e30e41 --- /dev/null +++ b/packages/cli/src/auth/callbackPage.ts @@ -0,0 +1,80 @@ +/** The page the browser lands on after sign-in: one design for the CLI's three endings. */ + +const MARK_A = + "M10.1851 57.8021L33.1145 73.8313C36.2202 75.9978 41.5173 73.5433 42.4816 69.4984L51.7611 30.4271C52.7253 26.3822 48.5802 23.9277 44.4602 26.0942L13.917 42.1235C6.96677 45.7676 4.97564 54.1579 10.1851 57.8021Z"; +const MARK_B = + "M87.5129 57.5141L56.9696 73.5433C52.8371 75.7098 48.7046 73.2553 49.6688 69.2104L58.9483 30.1391C59.9125 26.0942 65.2097 23.6397 68.3154 25.8062L91.2447 41.8354C96.4668 45.4796 94.4631 53.8699 87.5129 57.5141Z"; + +const STYLE = `:root{color-scheme:light dark;--bg:#f2f0ed;--ink:#1d1b18;--ink2:#5d5a55;--tile:#dff3ea;--tile-ink:#087a54;--bad:#fbe6e1;--bad-ink:#b3402e;--btn:#1d1b18;--btn-ink:#f2f0ed;--panel:#0c0c0e;--panel-ink:#f4f1ec} +@media(prefers-color-scheme:dark){:root{--bg:#201e1b;--ink:#f6f4f1;--ink2:#b5b1aa;--tile:#1f3a2f;--tile-ink:#37de91;--bad:#3a2420;--bad-ink:#f08a78;--btn:#f6f4f1;--btn-ink:#201e1b}} +*{box-sizing:border-box}html,body{height:100%;margin:0} +body{background:var(--bg);color:var(--ink);font:400 15px/1.45 "TT Norms",system-ui,-apple-system,"Segoe UI",sans-serif;-webkit-font-smoothing:antialiased;display:grid;grid-template-columns:minmax(0,1fr) minmax(0,1.25fr)} +.col{position:relative;display:flex;flex-direction:column;justify-content:center;padding:48px 32px 48px max(32px,min(7vw,96px))} +.brand{position:absolute;top:28px;left:32px;display:flex;align-items:center;gap:10px;font-weight:600;font-size:20px;letter-spacing:-.01em} +.tile{display:grid;place-items:center;width:44px;height:44px;border-radius:12px;background:var(--tile);color:var(--tile-ink);margin-bottom:20px} +.bad .tile{background:var(--bad);color:var(--bad-ink)} +h1{margin:0;font-size:30px;line-height:1.1;font-weight:600;letter-spacing:-.02em;text-wrap:balance} +p{margin:10px 0 0;color:var(--ink2);max-width:360px;text-wrap:pretty} +code{font:500 13px ui-monospace,SFMono-Regular,Menlo,monospace;color:var(--ink2)} +.panel{margin:14px;border-radius:24px;background:var(--panel);color:var(--panel-ink);display:flex;flex-direction:column;align-items:center;justify-content:center;gap:18px} +.panel b{font-size:26px;font-weight:600;letter-spacing:-.01em} +.panel small{color:rgb(244 241 236/.58);font-size:14px;margin-top:-10px} +.badge{position:relative} +.chk{display:none;position:absolute;right:-8px;bottom:-4px;width:22px;height:22px;border-radius:50%;background:#37de91;place-items:center} +.ok .chk{display:grid} +@media(max-width:760px){body{grid-template-columns:1fr;grid-template-rows:auto auto}.col{padding:84px 24px 32px}.brand{left:24px}.panel{min-height:300px;margin:12px}}`; + +const CHECK = + ''; +const CROSS = + ''; + +const CHK = + ''; + +function mark(width: number, height: number, id: string): string { + const gradient = (suffix: string, x1: string, y1: string, x2: string, y2: string): string => + ``; + return ``; +} + +function escapeHtml(s: string): string { + return s + .replace(/&/g, "&") + .replace(//g, ">") + .replace(/"/g, """) + .replace(/'/g, "'"); +} + +function page(args: { ok: boolean; title: string; text: string; detail?: string }): string { + const detail = args.detail ? `

${escapeHtml(args.detail)}

` : ""; + return `${escapeHtml(args.title)}
${mark(38, 23, "c")}HyperFrames
${args.ok ? CHECK : CROSS}

${escapeHtml(args.title)}

${escapeHtml(args.text)}

${detail}
`; +} + +export function connectedPage(): string { + return page({ + ok: true, + title: "You're connected", + text: "Return to your terminal. You can close this tab.", + }); +} + +/** A Cancel says "Not authorized"; any other ending is a failure and says so. */ +export function failurePage(code: string, description: string): string { + if (code === "access_denied") { + return page({ + ok: false, + title: "Not authorized", + text: "Nothing was connected. Run the login again from your terminal.", + }); + } + return page({ + ok: false, + title: "Couldn't connect", + text: + description || + "Something went wrong on the way back. Run the login again from your terminal.", + detail: code, + }); +} diff --git a/packages/cli/src/auth/loopback.test.ts b/packages/cli/src/auth/loopback.test.ts index a133c660a6..76d63c7705 100644 --- a/packages/cli/src/auth/loopback.test.ts +++ b/packages/cli/src/auth/loopback.test.ts @@ -27,7 +27,7 @@ describe("auth/loopback", () => { expect(result.redirectUri).toContain(redirect.host); expect(res.status).toBe(200); const body = await res.text(); - expect(body).toContain("Signed in"); + expect(body).toContain("You're connected"); }); async function expectRejection(args: { @@ -57,6 +57,29 @@ describe("auth/loopback", () => { }); }); + async function pageFor(query: string): Promise { + const handle = await startLoopback({ state: "s", timeoutMs: 5_000 }); + active = handle; + handle.result.catch(() => {}); + const res = await fetch(`${handle.redirectUri}?${query}`); + expect(res.status).toBe(400); + return res.text(); + } + + it("says Not authorized only when the person pressed Cancel", async () => { + const body = await pageFor("error=access_denied&state=s"); + expect(body).toContain("Not authorized"); + expect(body).not.toContain("Couldn't connect"); + }); + + it("says Couldn't connect, with the code, for a failure that is not a Cancel", async () => { + const body = await pageFor("error=server_error&error_description=try+later&state=s"); + expect(body).toContain("Couldn't connect"); + expect(body).toContain("server_error"); + expect(body).toContain("try later"); + expect(body).not.toContain("Not authorized"); + }); + it("rejects when code is missing from the callback", async () => { await expectRejection({ expectedState: "s", query: "state=s", pattern: /code/ }); }); diff --git a/packages/cli/src/auth/loopback.ts b/packages/cli/src/auth/loopback.ts index df88de1c3b..f145230f77 100644 --- a/packages/cli/src/auth/loopback.ts +++ b/packages/cli/src/auth/loopback.ts @@ -18,6 +18,7 @@ import { timingSafeEqual } from "node:crypto"; import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http"; import { AddressInfo } from "node:net"; +import { connectedPage, failurePage } from "./callbackPage.js"; export interface LoopbackOptions { /** Expected `state` value — flow fails if it doesn't match. */ @@ -137,14 +138,14 @@ function handleRequest( const error = params.get("error"); if (error) { const desc = params.get("error_description") ?? ""; - respond(res, 400, errorPage(error, desc)); + respond(res, 400, failurePage(error, desc)); rejectResult(new Error(`OAuth authorize returned error: ${error}${desc ? ` — ${desc}` : ""}`)); return; } const state = params.get("state"); if (!state || !stateMatches(state, expectedState)) { - respond(res, 400, errorPage("invalid_state", "State parameter did not match.")); + respond(res, 400, failurePage("invalid_state", "State parameter did not match.")); rejectResult(new Error("OAuth state mismatch — possible CSRF, aborting.")); return; } @@ -154,13 +155,13 @@ function handleRequest( respond( res, 400, - errorPage("missing_code", "Authorization code is missing from the redirect."), + failurePage("missing_code", "Authorization code is missing from the redirect."), ); rejectResult(new Error("OAuth redirect did not include `code`.")); return; } - respond(res, 200, successPage()); + respond(res, 200, connectedPage()); resolveResult({ code, redirectUri }); } @@ -191,26 +192,3 @@ function respond(res: ServerResponse, status: number, body: string): void { }) .end(body); } - -function successPage(): string { - return `Signed in to HyperFrames - -

You're signed in.

You can close this tab and return to your terminal.

`; -} - -function errorPage(code: string, description: string): string { - const safeCode = escapeHtml(code); - const safeDesc = escapeHtml(description); - return `Sign-in failed - -

Sign-in failed

${safeCode}

${safeDesc}

`; -} - -function escapeHtml(s: string): string { - return s - .replace(/&/g, "&") - .replace(//g, ">") - .replace(/"/g, """) - .replace(/'/g, "'"); -}