diff --git a/dashboard/e2e/audit-shell.spec.ts b/dashboard/e2e/audit-shell.spec.ts index 67210df8..c8067a79 100644 --- a/dashboard/e2e/audit-shell.spec.ts +++ b/dashboard/e2e/audit-shell.spec.ts @@ -50,6 +50,7 @@ async function mockApi(page: Page, init: Partial = {}): Promise { +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); }); diff --git a/dashboard/e2e/sign-in-methods.spec.ts b/dashboard/e2e/sign-in-methods.spec.ts new file mode 100644 index 00000000..f72e7c3b --- /dev/null +++ b/dashboard/e2e/sign-in-methods.spec.ts @@ -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$/); +}); diff --git a/dashboard/src/components/client-shell.tsx b/dashboard/src/components/client-shell.tsx index 918c6590..77f18f84 100644 --- a/dashboard/src/components/client-shell.tsx +++ b/dashboard/src/components/client-shell.tsx @@ -35,6 +35,7 @@ import { SetupBanner } from "@/components/setup-banner"; 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. */ @@ -407,12 +408,7 @@ export function ClientShell({ children }: { children: React.ReactNode }) { > Copy the link for your GitHub admin - - Then sign in with GitHub again - + {teams.filter((t) => t.status !== "deactivated").length > 0 && (
@@ -838,6 +834,20 @@ function SignOutIcon() { * 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 ( + + Then sign in with GitHub again + + ); +} + function SignInProblemNotice({ problem }: { problem: SignInProblem }) { const [chromeHref, setChromeHref] = useState(null); useEffect(() => { @@ -852,7 +862,7 @@ function SignInProblemNotice({ problem }: { problem: SignInProblem }) { Try again )} - {!problem.canRetry && ( + {problem.code === "in_app_browser" && ( <> window.location.origin + "/"} label="Copy the link" copiedLabel="Link copied" variant="link" /> {chromeHref && ( @@ -1009,6 +1019,9 @@ function AuthScreen({ onAuth }: { onAuth: (user: User) => void }) { 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); @@ -1257,9 +1270,10 @@ function AuthScreen({ onAuth }: { onAuth: (user: User) => void }) { ยท anyone can sign up
)} -
+ {providers && ( +
{/* A self-hosted install has no Google sign-in. */} - {!selfHosted && ( + {methods.google && ( void }) { Continue with Google )} + {/* GitHub only with an OAuth client of this install's own. */} + {methods.github && ( void }) { Continue with GitHub + )}
+ )} + {providers && (
or
+ )}
{ e.preventDefault(); handleLogin(); }} className="space-y-3"> { setEmail(e.target.value); setError(""); setMessage(""); }} diff --git a/dashboard/src/hooks/use-sign-in-methods.ts b/dashboard/src/hooks/use-sign-in-methods.ts new file mode 100644 index 00000000..d681608c --- /dev/null +++ b/dashboard/src/hooks/use-sign-in-methods.ts @@ -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(() => 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; +} diff --git a/dashboard/src/lib/api.ts b/dashboard/src/lib/api.ts index 394a8e31..b9e5b362 100644 --- a/dashboard/src/lib/api.ts +++ b/dashboard/src/lib/api.ts @@ -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("/auth/methods"), me: () => request<{ user_id: string; diff --git a/dashboard/src/lib/sign-in-errors.ts b/dashboard/src/lib/sign-in-errors.ts index 7175b4bb..f2dadc6d 100644 --- a/dashboard/src/lib/sign-in-errors.ts +++ b/dashboard/src/lib/sign-in-errors.ts @@ -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: