caelum0x 2bcb0101ca feat(console): email code sign-in through Scribase Auth
The console could only sign in with Google or GitHub, so it could not open
before both OAuth apps exist (owner item Y-2). Add email sign-in: Scribase
Auth mails a one-time code (POST /otp), the console redeems it (POST /verify),
reads the user from the auth server, checks it matches the address, and opens
the same session as the OAuth path. Same-origin checks on both posts; inputs
validated before any call. RAILYARD_OAUTH_PROVIDERS picks the OAuth buttons
(default google,github; empty = email only). 7 new tests, console 154/154,
tsc clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 00:26:59 +03:00

50 lines
2.6 KiB
TypeScript

import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { currentSession } from "../lib/current.ts";
import { EMAIL_COOKIE, oauthProvidersFrom } from "../lib/scribase-auth.ts";
export const dynamic = "force-dynamic";
const LABELS = { google: "Continue with Google", github: "Continue with GitHub" } as const;
function notice(signin?: string): string | null {
if (signin === "email") return "Type a full email address.";
if (signin === "code") return "That code did not work. Check the newest email and try again.";
if (signin) return "Sign-in did not finish. Try again.";
return null;
}
export default async function SignIn({ searchParams }: { searchParams: { signin?: string; step?: string } }) {
if (await currentSession()) redirect("/apps");
const providers = oauthProvidersFrom(process.env.RAILYARD_OAUTH_PROVIDERS);
const codeStep = searchParams.step === "code" && !!cookies().get(EMAIL_COOKIE)?.value;
const message = notice(searchParams.signin);
return (
<main className="center">
<section className="card" style={{ width: 380, textAlign: "center" }}>
<h1>Railyard</h1>
<p className="muted">Ship it, see who uses it, from your coding agent.</p>
{message ? <p className="error">{message}</p> : null}
{codeStep ? (
<form method="post" action="/auth/email/verify" style={{ display: "grid", gap: 10, marginTop: 18 }}>
<p className="muted">We sent a sign-in code to your email.</p>
<input type="text" name="code" inputMode="text" autoComplete="one-time-code" placeholder="Code" required aria-label="Sign-in code" />
<button className="primary" style={{ justifyContent: "center" }} type="submit">Sign in</button>
<a className="ghost" style={{ justifyContent: "center" }} href="/">Use another address</a>
</form>
) : (
<div style={{ display: "grid", gap: 10, marginTop: 18 }}>
{providers.map((p, i) => (
<a key={p} className={i === 0 ? "primary" : "ghost"} style={{ justifyContent: "center" }} href={`/auth/start?provider=${p}`}>{LABELS[p]}</a>
))}
<form method="post" action="/auth/email" style={{ display: "grid", gap: 10 }}>
<input type="email" name="email" autoComplete="email" placeholder="you@company.com" required aria-label="Email" />
<button className={providers.length ? "ghost" : "primary"} style={{ justifyContent: "center" }} type="submit">Email me a sign-in code</button>
</form>
</div>
)}
</section>
</main>
);
}