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
1 change: 1 addition & 0 deletions dashboard/e2e/audit-shell.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ async function mockApi(page: Page, init: Partial<Backend> = {}): Promise<Backend
if (db.me !== 200) return route.fulfill({ status: db.me, body: "" });
return json(route, { user_id: "u_1", team_id: "t_1", github_login: null, email: db.email, avatar_url: "", role: db.role, status: db.status });
}
if (p === "/auth/methods") return json(route, { password: true, google: true, github: true });
if (p === "/api/usage") return json(route, { month: "2026-01", tokens_in: 0, tokens_out: 0, total_runs: 0, ...db.usage });
if (p === "/api/banners") return json(route, { banners: db.banners });
if (p === "/api/support/badge") return json(route, { waiting: 0, queue_open: null });
Expand Down
6 changes: 4 additions & 2 deletions dashboard/e2e/hybrid-jira-email.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,11 @@ test("Deliveries shows GitHub and Jira relay rows and email by kind, with Cognit
await expect(page.getByTestId("overview-email-cognito")).toContainText("about 50 a day");
});

test("a self-hosted install's sign-in has no Google button; the hosted service keeps it", async ({ page }) => {
test("a self-hosted install's sign-in has no Google button, and GitHub only when the API lists it", async ({ page }) => {
await setup(page, SELF, { signedIn: false });
await page.goto("/");
await expect(page.getByRole("link", { name: "Continue with GitHub" })).toBeVisible();
await expect(page.getByLabel("Email")).toBeVisible();
// This mock has no /auth/methods: a self-hosted install then offers email and password only.
await expect(page.getByRole("link", { name: "Continue with GitHub" })).toHaveCount(0);
await expect(page.getByRole("link", { name: "Continue with Google" })).toHaveCount(0);
});
112 changes: 112 additions & 0 deletions dashboard/e2e/sign-in-methods.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { expect, test, type Page, type Route } from "@playwright/test";

// The sign-in screen shows only the ways in that work on this deployment, as
// the API's GET /auth/methods says. A self-hosted install on Coderhelm's
// GitHub App (hybrid) has no GitHub OAuth client of its own, so it offers no
// GitHub button: before this, the button sent people to GitHub with an empty
// client_id and GitHub answered 404. Every API call is answered here.

type Methods = { password: boolean; google: boolean; github: boolean };

async function mockApi(page: Page, opts: { selfHosted: boolean; methods: Methods | "missing"; status?: string }) {
const calls: string[] = [];
const json = (route: Route, body: unknown, status = 200) =>
route.fulfill({ status, contentType: "application/json", body: JSON.stringify(body) });
if (opts.selfHosted) {
await page.route((url) => url.pathname === "/runtime-config.js", (route) =>
route.fulfill({
contentType: "text/javascript",
body: `window.__CODERHELM_CONFIG__ = ${JSON.stringify({ apiUrl: "", streamUrl: "", selfHosted: true, siteUrl: null, supportEmail: "it@acme.example" })};`,
}),
);
}
await page.route((url) => url.hostname !== "localhost", (route) => route.abort());
await page.route((url) => url.hostname === "localhost" && /^\/(api|auth)\//.test(url.pathname), (route) => {
const p = new URL(route.request().url()).pathname;
calls.push(p);
if (p === "/auth/methods") return opts.methods === "missing" ? json(route, { error: "Not found" }, 404) : json(route, opts.methods);
if (p === "/api/me") {
return opts.status
? json(route, { user_id: "u_1", team_id: "t_1", github_login: "a-dev", email: "dev@acme.example", avatar_url: "", role: "owner", status: opts.status })
: json(route, { error: "unauthorized" }, 401);
}
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 }] });
return json(route, { error: "Not mocked" }, 404);
});
return { calls };
}

const github = (page: Page) => page.getByRole("link", { name: "Continue with GitHub" });
const google = (page: Page) => page.getByRole("link", { name: "Continue with Google" });

test("hybrid install with no GitHub client: email and password only, no GitHub button and no 'or'", async ({ page }) => {
const { calls } = await mockApi(page, { selfHosted: true, methods: { password: true, google: false, github: false } });
await page.goto("/");
await expect(page.getByLabel("Email")).toBeVisible();
await expect(page.getByRole("button", { name: "Sign in" })).toBeVisible();
await expect.poll(() => calls.includes("/auth/methods")).toBe(true);
await expect(github(page)).toHaveCount(0);
await expect(google(page)).toHaveCount(0);
await expect(page.getByTestId("sign-in-providers")).toHaveCount(0);
await expect(page.getByText("or", { exact: true })).toHaveCount(0);
});

test("self-hosted install with its own GitHub client: the GitHub button goes to the API's sign-in", async ({ page }) => {
await mockApi(page, { selfHosted: true, methods: { password: true, google: false, github: true } });
await page.goto("/");
await expect(github(page)).toBeVisible();
await expect(github(page)).toHaveAttribute("href", /^\/auth\/github$/);
await expect(google(page)).toHaveCount(0);
await expect(page.getByText("or", { exact: true })).toBeVisible();
});

test("self-hosted install whose API has no method list yet: no GitHub button", async ({ page }) => {
await mockApi(page, { selfHosted: true, methods: "missing" });
await page.goto("/");
await expect(page.getByLabel("Email")).toBeVisible();
await expect(github(page)).toHaveCount(0);
});

test("hosted: the list drives both buttons, and an older API keeps both", async ({ page }) => {
await mockApi(page, { selfHosted: false, methods: { password: true, google: true, github: true } });
await page.goto("/");
await expect(google(page)).toBeVisible();
await expect(github(page)).toHaveAttribute("href", /\/auth\/github$/);

await page.unrouteAll({ behavior: "ignoreErrors" });
await mockApi(page, { selfHosted: false, methods: { password: true, google: true, github: false } });
await page.goto("/");
await expect(google(page)).toBeVisible();
await expect(github(page)).toHaveCount(0);

await page.unrouteAll({ behavior: "ignoreErrors" });
await mockApi(page, { selfHosted: false, methods: "missing" });
await page.goto("/");
await expect(google(page)).toBeVisible();
await expect(github(page)).toBeVisible();
});

test("GET /auth/github with no client lands back here with a plain sentence and no Try again", async ({ page }) => {
await mockApi(page, { selfHosted: true, methods: { password: true, google: false, github: false } });
await page.goto("/?signin_error=not_set_up&provider=github");
const notice = page.getByTestId("sign-in-problem");
await expect(notice).toContainText("GitHub sign-in isn't set up on this Coderhelm. Sign in with your email and password.");
await expect(notice.getByRole("link", { name: "Try again" })).toHaveCount(0);
await expect(notice.getByRole("button", { name: "Copy the link" })).toHaveCount(0);
await expect(github(page)).toHaveCount(0);
await expect(page.getByLabel("Email")).toBeVisible();
});

test("removed GitHub app: 'sign in with GitHub again' only where GitHub sign-in works", async ({ page }) => {
await mockApi(page, { selfHosted: true, methods: { password: true, google: false, github: false }, status: "deactivated" });
await page.goto("/");
await expect(page.getByRole("heading", { name: "The GitHub app was removed" })).toBeVisible();
await expect(page.getByRole("link", { name: "Then sign in with GitHub again" })).toHaveCount(0);

await page.unrouteAll({ behavior: "ignoreErrors" });
await mockApi(page, { selfHosted: true, methods: { password: true, google: false, github: true }, status: "deactivated" });
await page.goto("/");
await expect(page.getByRole("link", { name: "Then sign in with GitHub again" })).toHaveAttribute("href", /^\/auth\/github$/);
});
38 changes: 29 additions & 9 deletions dashboard/src/components/client-shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import { CopyButton } from "./copy-button";
import { openInChromeHref, signInProblem, type SignInProblem } from "@/lib/sign-in-errors";
import { TwoFactorSetup } from "./two-factor-setup";
import { useSignInMethods } from "@/hooks/use-sign-in-methods";
import type { MyTwoFactor } from "@/lib/api";

/** Sign out everywhere the same way: end the session, then the sign-in screen. */
Expand Down Expand Up @@ -299,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 @@ -407,12 +408,7 @@
>
Copy the link for your GitHub admin
</button>
<a
href={`${apiBase()}/auth/github`}
className={`text-sm text-zinc-300 hover:text-zinc-100 underline transition-colors rounded ${focusRing}`}
>
Then sign in with GitHub again
</a>
<GithubSignInAgain />
</div>
{teams.filter((t) => t.status !== "deactivated").length > 0 && (
<div className="mt-2">
Expand Down Expand Up @@ -838,6 +834,20 @@
* do next: Try again, or, inside another app's browser, open the page in a
* real browser.
*/
/** After the app is reinstalled: GitHub sign-in again, where this deployment has it. */
function GithubSignInAgain() {
const methods = useSignInMethods();
if (!methods.github) return null;
return (
<a
href={`${apiBase()}/auth/github`}
className={`text-sm text-zinc-300 hover:text-zinc-100 underline transition-colors rounded ${focusRing}`}
>
Then sign in with GitHub again
</a>
);
}

function SignInProblemNotice({ problem }: { problem: SignInProblem }) {
const [chromeHref, setChromeHref] = useState<string | null>(null);
useEffect(() => {
Expand All @@ -852,7 +862,7 @@
Try again
</a>
)}
{!problem.canRetry && (
{problem.code === "in_app_browser" && (
<>
<CopyButton text={() => window.location.origin + "/"} label="Copy the link" copiedLabel="Link copied" variant="link" />
{chromeHref && (
Expand Down Expand Up @@ -1009,6 +1019,9 @@
const site = siteUrl();
// A self-hosted install: one organization, joined only by invite.
const selfHosted = isSelfHosted();
// Only the ways in that work here (the API says which).
const methods = useSignInMethods();
const providers = methods.google || methods.github;
// A self-hosted install has no terms to agree to (no site): the box is pre-ticked and hidden.
useEffect(() => {
if (!site) setAgreedTos(true);
Expand Down Expand Up @@ -1257,9 +1270,10 @@
<span className="text-xs text-zinc-400">· anyone can sign up</span>
</div>
)}
<div className="space-y-3">
{providers && (
<div className="space-y-3" data-testid="sign-in-providers">
{/* A self-hosted install has no Google sign-in. */}
{!selfHosted && (
{methods.google && (
<a
href={`${apiBase()}/auth/google`}
className="flex w-full items-center justify-center gap-2 rounded-lg bg-white px-4 py-2.5 text-sm font-medium text-zinc-900 hover:bg-zinc-200 transition-colors"
Expand All @@ -1268,20 +1282,26 @@
Continue with Google
</a>
)}
{/* GitHub only with an OAuth client of this install's own. */}
{methods.github && (
<a
href={`${apiBase()}/auth/github`}
className="flex w-full items-center justify-center gap-2 rounded-lg bg-zinc-800 border border-zinc-700 px-4 py-2.5 text-sm font-medium text-zinc-100 hover:bg-zinc-700 transition-colors"
>
<svg className="w-4 h-4" viewBox="0 0 24 24" fill="currentColor"><path d="M12 0C5.37 0 0 5.37 0 12c0 5.31 3.435 9.795 8.205 11.385.6.105.825-.255.825-.57 0-.285-.015-1.23-.015-2.235-3.015.555-3.795-.735-4.035-1.41-.135-.345-.72-1.41-1.23-1.695-.42-.225-1.02-.78-.015-.795.945-.015 1.62.87 1.845 1.23 1.08 1.815 2.805 1.305 3.495.99.105-.78.42-1.305.765-1.605-2.67-.3-5.46-1.335-5.46-5.925 0-1.305.465-2.385 1.23-3.225-.12-.3-.54-1.53.12-3.18 0 0 1.005-.315 3.3 1.23.96-.27 1.98-.405 3-.405s2.04.135 3 .405c2.295-1.56 3.3-1.23 3.3-1.23.66 1.65.24 2.88.12 3.18.765.84 1.23 1.905 1.23 3.225 0 4.605-2.805 5.625-5.475 5.925.435.375.81 1.095.81 2.22 0 1.605-.015 2.895-.015 3.3 0 .315.225.69.825.57A12.02 12.02 0 0 0 24 12c0-6.63-5.37-12-12-12z"/></svg>
Continue with GitHub
</a>
)}
</div>
)}

{providers && (
<div className="flex items-center gap-3">
<div className="flex-1 h-px bg-zinc-800" />
<span className="text-xs text-zinc-400">or</span>
<div className="flex-1 h-px bg-zinc-800" />
</div>
)}

<form onSubmit={(e) => { e.preventDefault(); handleLogin(); }} className="space-y-3">
<input type="email" name="email" id="login-email" autoComplete="email" aria-label="Email" placeholder="Email" value={email} onChange={(e) => { setEmail(e.target.value); setError(""); setMessage(""); }}
Expand Down
37 changes: 37 additions & 0 deletions dashboard/src/hooks/use-sign-in-methods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"use client";

import { useEffect, useState } from "react";
import { api, type SignInMethods } from "@/lib/api";
import { isSelfHosted } from "@/lib/runtime-config";

// The sign-in methods that work on this deployment, from the API
// (`GET /auth/methods`): the sign-in screen shows these and no others, so it
// never offers a button that ends on someone else's error page. A
// self-hosted install has GitHub only with an OAuth client of its own; an
// install on Coderhelm's GitHub App (hybrid) has none.
//
// Until the API answers, and when it can't (an older gateway), the hosted
// service keeps Google and GitHub and a self-hosted install shows email and
// password only.

export function fallbackSignInMethods(selfHosted: boolean): SignInMethods {
return { password: true, google: !selfHosted, github: !selfHosted };
}

export function useSignInMethods(): SignInMethods {
const [methods, setMethods] = useState<SignInMethods>(() => fallbackSignInMethods(isSelfHosted()));
useEffect(() => {
let live = true;
api
.signInMethods()
.then((m) => {
if (!live) return;
setMethods({ password: m.password !== false, google: m.google === true, github: m.github === true });
})
.catch(() => {});
return () => {
live = false;
};
}, []);
return methods;
}
8 changes: 8 additions & 0 deletions dashboard/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,16 @@ export interface SignInSettings {
your_session_has_2fa?: boolean;
}

/** The sign-in methods that work on this deployment (`GET /auth/methods`, public). */
export interface SignInMethods {
password: boolean;
google: boolean;
github: boolean;
}

export const api = {
// Auth
signInMethods: () => request<SignInMethods>("/auth/methods"),
me: () =>
request<{
user_id: string;
Expand Down
7 changes: 7 additions & 0 deletions dashboard/src/lib/sign-in-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ export function signInProblem(search: string): SignInProblem | null {
return { code, provider, sentence: `Sign-in with ${provider ? NAMES[provider] : "the provider"} was cancelled before it finished.`, canRetry: true };
case "provider":
return { code, provider, sentence: `${name} didn't confirm your sign-in.`, canRetry: true };
case "not_set_up":
return {
code,
provider,
sentence: `${provider ? NAMES[provider] : "That"} sign-in isn't set up on this Coderhelm. Sign in with your email and password.`,
canRetry: false,
};
case "server":
return { code, provider, sentence: "Something went wrong on our side while signing you in.", canRetry: true };
default:
Expand Down
Loading