Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 65 additions & 43 deletions dashboard/e2e/sign-in-methods.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ async function mockApi(page: Page, opts: { selfHosted: boolean; methods: Methods
: json(route, { error: "unauthorized" }, 401);
}
if (p === "/auth/forgot-password" && opts.forgot) return json(route, opts.forgot);
if (p === "/auth/signup") return json(route, { status: "verification_required", message: "If that email can join here, we've sent a verification code." });
if (p === "/api/usage") return json(route, { month: "2026-01", tokens_in: 0, tokens_out: 0, total_runs: 0, total_tokens: 0, max_tokens: 0 });
if (p === "/api/banners") return json(route, { banners: [] });
if (p === "/api/teams") return json(route, { teams: [{ team_id: "t_1", org: "acme", status: "deactivated", current: true }] });
Expand Down Expand Up @@ -127,8 +128,9 @@ test("removed GitHub app: 'sign in with GitHub again' only where GitHub sign-in
await expect(page.getByRole("link", { name: "Then sign in with GitHub again" })).toHaveAttribute("href", /^\/auth\/github$/);
});

// Forgot password on a self-hosted install says what happened for that email, and where the
// email comes from, so someone whose mail filter holds it back can ask IT to allow it.
// Forgot password gives one answer for every email, so the page can't be used to learn who has
// an account here or is on the team. It names both senders, so someone whose mail filter holds
// one back can ask IT to allow it.
const SENDERS = { setup: "coderhelm@acme.example", reset_codes: "no-reply@verificationemail.com" };

async function askForReset(page: Page, email: string) {
Expand All @@ -138,57 +140,77 @@ async function askForReset(page: Page, email: string) {
await page.getByRole("button", { name: "Send", exact: true }).click();
}

test("forgot password, account here: the code is on its way from Amazon Cognito's address", async ({ page }) => {
const { calls } = await mockApi(page, {
selfHosted: true,
methods: { password: true, google: false, github: false, senders: SENDERS },
forgot: { status: "code_sent", message: "We sent a reset code to jo@acme.example.", email: "jo@acme.example" },
});
await askForReset(page, "jo@acme.example");
await expect(page.getByRole("status").filter({ hasText: "sent" })).toHaveText("We sent a reset code to jo@acme.example.");
await expect(page.getByTestId("sender-note")).toContainText(
"The reset code comes from no-reply@verificationemail.com (Amazon Cognito, which signs people in here).",
);
await expect(page.getByTestId("sender-note")).toContainText("ask IT to allow that address");
await expect(page.getByLabel("Reset code")).toBeVisible();
expect(calls).toContain("/auth/forgot-password");
});
/** The page's error box (not Next's route announcer, which is an alert too). */
const errorBox = (page: Page) => page.getByTestId("auth-error");

test("forgot password, on the team with no account here: a set-up link instead, from the install's sender", async ({ page }) => {
await mockApi(page, {
selfHosted: true,
methods: { password: true, google: false, github: false, senders: SENDERS },
forgot: { status: "setup_sent", message: "You don't have an account here yet. We sent you a set-up link instead." },
});
await askForReset(page, "al@acme.example");
await expect(page.getByRole("status").filter({ hasText: "sent" })).toHaveText("You don't have an account here yet. We sent you a set-up link instead.");
await expect(page.getByTestId("sender-note")).toContainText("The set-up link comes from coderhelm@acme.example.");
await expect(page.getByTestId("sender-note")).not.toContainText("verificationemail.com");
// No code to type: the link in the email sets up the account.
await expect(page.getByLabel("Reset code")).toHaveCount(0);
});
/** What the page shows after asking: the message, the sender lines, and what comes next. */
async function shown(page: Page) {
await expect(page.getByLabel("Reset code")).toBeVisible();
return {
message: await page.getByRole("status").innerText(),
senders: await page.getByTestId("sender-note").locator("p").allInnerTexts(),
setupNote: await page.getByTestId("setup-link-instead").innerText(),
errors: await errorBox(page).count(),
};
}

test("forgot password, not on this install: says so, sends nothing, no sender line", async ({ page }) => {
await mockApi(page, {
selfHosted: true,
methods: { password: true, google: false, github: false, senders: SENDERS },
forgot: { status: "no_account", message: "There's no account for who@else.example on this CoderHelm. Ask an admin to add you." },
// Whatever a server answers (the one `sent`, or a per-case status an older gateway gave), the
// page reads the same: has an account, on the team without one, invited, not on this install.
const REPLIES: ForgotReply[] = [
{ status: "sent", message: "If that email can sign in here, we've sent a reset code or a set-up link." },
{ status: "code_sent", message: "We sent a reset code to jo@acme.example.", email: "jo@acme.example" },
{ status: "setup_sent", message: "You don't have an account here yet. We sent you a set-up link instead." },
{ status: "invited", message: "jo@acme.example has an invite here but no account yet." },
{ status: "no_account", message: "There's no account for jo@acme.example on this CoderHelm. Ask an admin to add you." },
];

test("forgot password: the same message, senders and next step for every case", async ({ page }) => {
const seen = [];
for (const forgot of REPLIES) {
const { calls } = await mockApi(page, {
selfHosted: true,
methods: { password: true, google: false, github: false, senders: SENDERS },
forgot,
});
await askForReset(page, "jo@acme.example");
seen.push(await shown(page));
expect(calls).toContain("/auth/forgot-password");
await page.unrouteAll({ behavior: "ignoreErrors" });
}
expect(seen[0]).toEqual({
message: "If jo@acme.example can sign in here, we've sent a reset code or a set-up link.",
senders: [
"Reset codes come from no-reply@verificationemail.com (Amazon Cognito, which signs people in here).",
"Set-up links come from coderhelm@acme.example.",
"Not in your inbox after a few minutes? Check spam or quarantine, or ask IT to allow those addresses.",
],
setupNote: "Got a set-up link instead of a code? There's no code to enter: open the link in that email to set up your account.",
errors: 0,
});
await askForReset(page, "who@else.example");
await expect(page.getByRole("alert").filter({ hasText: "no account" })).toHaveText(
"There's no account for who@else.example on this CoderHelm. Ask an admin to add you.",
);
await expect(page.getByTestId("sender-note")).toHaveCount(0);
await expect(page.getByLabel("Reset code")).toHaveCount(0);
for (const s of seen) expect(s).toEqual(seen[0]);
});

test("forgot password with an SES sender for Cognito: that address, not Cognito's", async ({ page }) => {
await mockApi(page, {
selfHosted: true,
methods: { password: true, google: false, github: false, senders: { setup: "coderhelm@acme.example", reset_codes: "noreply@acme.example" } },
forgot: { status: "code_sent", message: "We sent a reset code to jo@acme.example." },
forgot: REPLIES[0],
});
await askForReset(page, "jo@acme.example");
await expect(page.getByTestId("sender-note")).toContainText("The reset code comes from noreply@acme.example.");
await expect(page.getByTestId("sender-note")).toContainText("Reset codes come from noreply@acme.example.");
await expect(page.getByTestId("sender-note")).not.toContainText("Amazon Cognito");
});

test("self-hosted sign-up from an invite link: one answer, whoever the email is", async ({ page }) => {
const { calls } = await mockApi(page, { selfHosted: true, methods: { password: true, google: false, github: false, senders: SENDERS } });
await page.goto("/?invite=1");
await page.getByLabel("Email").fill("jo@acme.example");
await page.getByLabel("Password, at least 8 characters").fill("Acme2026x");
await page.getByRole("button", { name: "Create account" }).click();
await expect(page.getByRole("status")).toHaveText(
"If jo@acme.example can join here, we've sent a verification code. No code after a few minutes? If you already have an account, sign in or use Forgot password; otherwise ask an admin to invite you.",
);
await expect(page.getByLabel("Verification code")).toBeVisible();
await expect(errorBox(page)).toHaveCount(0);
expect(calls).toContain("/auth/signup");
});
79 changes: 48 additions & 31 deletions dashboard/src/components/client-shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import Link from "next/link";
import { usePathname, useRouter } from "next/navigation";
import { type ReactNode, useEffect, useRef, useState } from "react";
import { api, ApiError, TEAM_MOVED, TWO_FACTOR_REQUIRED, movedUrl, type ForgotStatus, type SignInSenders, type UsageInfo, type Banner, type TeamInfo } from "@/lib/api";
import { api, ApiError, TEAM_MOVED, TWO_FACTOR_REQUIRED, movedUrl, type SignInSenders, type UsageInfo, type Banner, type TeamInfo } from "@/lib/api";
import { pushToDataLayer } from "@/lib/gtm";
import { formatTokens, limitName, reachedLimit, USAGE_CHANGED, usageBreakdown, usageLimits } from "@/lib/usage";
import { TokenMeters } from "@/components/token-meters";
Expand Down Expand Up @@ -300,7 +300,7 @@
if (e instanceof ApiError && e.code === "team_moved") showMoved(movedUrl(e.url));
setAuthChecked(true);
});
}, []);

Check warning on line 303 in dashboard/src/components/client-shell.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

React Hook useEffect has a missing dependency: 'loadTwoFactor'. Either include it or remove the dependency array

// Refresh usage on route changes so token count stays current
useEffect(() => {
Expand Down Expand Up @@ -1000,23 +1000,35 @@
const COGNITO_DEFAULT_SENDER = "no-reply@verificationemail.com";

/**
* Where the email that forgot-password just sent comes from, so a person whose mail filter
* holds it back can ask IT to allow that address. Shown only when the install said who sends.
* What the sign-in page says after Forgot password, the same for every email: it never says
* whether the email has an account here, is on the team, is invited, or is unknown.
*/
function SenderNote({ status, senders }: { status: ForgotStatus | null; senders?: SignInSenders }) {
if (!status || !senders) return null;
let line = "";
if (status === "code_sent" && senders.reset_codes) {
function forgotSentMessage(email: string, selfHosted: boolean): string {
return selfHosted
? `If ${email} can sign in here, we've sent a reset code or a set-up link.`
: `If ${email} can sign in here, we've sent a reset code.`;
}

/**
* Who sends both emails Forgot password can send, so a person whose mail filter holds one back
* can ask IT to allow it. The same lines whatever the email; shown when the install said who sends.
*/
function SenderNote({ senders }: { senders?: SignInSenders }) {
if (!senders) return null;
const lines: string[] = [];
if (senders.reset_codes) {
const who = senders.reset_codes === COGNITO_DEFAULT_SENDER ? " (Amazon Cognito, which signs people in here)" : "";
line = `The reset code comes from ${senders.reset_codes}${who}.`;
} else if ((status === "setup_sent" || status === "setup_recent") && senders.setup) {
line = `The set-up link comes from ${senders.setup}.`;
lines.push(`Reset codes come from ${senders.reset_codes}${who}.`);
}
if (!line) return null;
if (senders.setup) lines.push(`Set-up links come from ${senders.setup}.`);
if (lines.length === 0) return null;
return (
<p data-testid="sender-note" className="w-full text-xs text-zinc-400">
{line} Not in your inbox after a few minutes? Check spam or quarantine, or ask IT to allow that address.
</p>
<div data-testid="sender-note" className="w-full space-y-1 text-xs text-zinc-400">
{lines.map((l) => <p key={l}>{l}</p>)}
<p>
Not in your inbox after a few minutes? Check spam or quarantine, or ask IT to allow {lines.length > 1 ? "those addresses" : "that address"}.
</p>
</div>
);
}

Expand All @@ -1034,8 +1046,8 @@
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
const [message, setMessage] = useState("");
// What forgot-password last did (a self-hosted install says which case it was).
const [forgotStatus, setForgotStatus] = useState<ForgotStatus | null>(null);
// Forgot password was asked for (its answer is the same for every email).
const [forgotSent, setForgotSent] = useState(false);
const [problem, setProblem] = useState<SignInProblem | null>(null);
// How a self-hosted install's account form was opened: an admin's invite email or the
// installer's first-admin link. Without either it has no open sign-up.
Expand Down Expand Up @@ -1104,7 +1116,13 @@
try {
await api.signup(email, password);
setView("verify");
setMessage("Check your email for a verification code.");
// A self-hosted install answers the same for every email (invited, already with an
// account, or not let in), so the page can't say which either.
setMessage(
selfHosted
? `If ${email} can join here, we've sent a verification code. No code after a few minutes? If you already have an account, sign in or use Forgot password; otherwise ask an admin to invite you.`
: "Check your email for a verification code.",
);
} catch (e: unknown) {
const msg = e instanceof Error ? e.message : "";
if (e instanceof ApiError && e.code === "not_invited") {
Expand Down Expand Up @@ -1182,19 +1200,13 @@
};

const handleForgot = async () => {
setError(""); setMessage(""); setForgotStatus(null); setLoading(true);
setError(""); setMessage(""); setForgotSent(false); setLoading(true);
try {
const r = await api.forgotPassword(email);
setForgotStatus(r.status);
if (r.status === "code_sent" || r.status === "sent") {
setView("reset");
setMessage(r.status === "sent" ? "If an account exists, a reset code was sent to your email." : r.message);
} else if (r.status === "no_account") {
setError(r.message);
} else {
// A set-up link (sent now or within the hour), or an invite to use: no code to enter.
setMessage(r.message);
}
// One answer for every email: the page always moves on to the code.
await api.forgotPassword(email);
setForgotSent(true);
setView("reset");
setMessage(forgotSentMessage(email, selfHosted));
} catch (e) {
setError(e instanceof ApiError ? e.message : "We couldn't send a reset code. Check your connection and try again.");
} finally { setLoading(false); }
Expand Down Expand Up @@ -1283,7 +1295,7 @@
{problem && view === "login" && <SignInProblemNotice problem={problem} />}

{error && (
<div role="alert" className="mt-2 w-full rounded-lg bg-red-500/10 border border-red-500/20 px-4 py-2.5 text-sm text-red-400">
<div role="alert" data-testid="auth-error" className="mt-2 w-full rounded-lg bg-red-500/10 border border-red-500/20 px-4 py-2.5 text-sm text-red-400">
{error}
</div>
)}
Expand All @@ -1294,7 +1306,7 @@
</div>
)}

{(view === "forgot" || view === "reset") && <SenderNote status={forgotStatus} senders={methods.senders} />}
{view === "reset" && forgotSent && <SenderNote senders={methods.senders} />}

{/* ── Login ── */}
{view === "login" && (
Expand Down Expand Up @@ -1472,6 +1484,11 @@
{view === "reset" && (
<div className="w-full space-y-4">
<p className="text-sm text-zinc-400 text-center">Enter the code and your new password</p>
{selfHosted && (
<p className="text-xs text-zinc-400 text-center" data-testid="setup-link-instead">
Got a set-up link instead of a code? There&apos;s no code to enter: open the link in that email to set up your account.
</p>
)}
<div className="space-y-3">
<input type="text" inputMode="numeric" autoComplete="one-time-code" aria-label="Reset code" placeholder="Reset code" value={code} onChange={(e) => { setCode(e.target.value); setError(""); }}
className="w-full rounded-lg bg-zinc-900 border border-zinc-800 px-4 py-2.5 text-sm text-zinc-100 placeholder-zinc-500 focus:outline-none focus:border-zinc-600 text-center tracking-widest" />
Expand Down
8 changes: 4 additions & 4 deletions dashboard/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ export interface SignInSenders {
}

/**
* What forgot-password did for an email. A self-hosted install says which case it was;
* the hosted service answers `sent` for every email.
* Forgot-password's answer: `sent`, the same for every email, so it never says whether one has
* an account or is on the team.
*/
export type ForgotStatus = "code_sent" | "setup_sent" | "setup_recent" | "invited" | "no_account" | "sent";
export type ForgotStatus = "sent";

export const api = {
// Auth
Expand Down Expand Up @@ -231,7 +231,7 @@ export const api = {
body: JSON.stringify({ email, code }),
}),
forgotPassword: (email: string) =>
request<{ status: ForgotStatus; message: string; email?: string }>("/auth/forgot-password", {
request<{ status: ForgotStatus; message: string }>("/auth/forgot-password", {
method: "POST",
body: JSON.stringify({ email }),
}),
Expand Down
Loading