Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

72 linesCodeBlameRaw
1import { Form, Link, redirect } from "react-router";
2
3import type { Route } from "./+types/login-two-factor";
4import { AuthCard } from "../components/auth-card";
5import { TwoFactorCodeField } from "../components/one-time-code";
6import { ErrorText, SubmitButton } from "../components/ui";
7import { PENDING_COOKIE, TWO_FACTOR_COOKIE, cookie, readCookie } from "../lib/github";
8import { githubSignIn } from "../lib/github.server";
9import { page } from "../lib/meta";
10import { identity } from "../lib/services.server";
11import { assertSameOrigin, clientOf, getViewer, nextPath, startSession } from "../lib/session.server";
12
13export function meta(args: Route.MetaArgs) {
14 return page(args, { title: "Two-factor authentication · g1t" });
15}
16
17/** Only reached with a sign-in waiting for its code; anyone else signs in first. */
18export async function loader({ request, context }: Route.LoaderArgs) {
19 if (getViewer(context)) throw redirect(nextPath(request));
20 if (!readCookie(request.headers.get("cookie"), TWO_FACTOR_COOKIE)) {
21 const next = nextPath(request);
22 throw redirect(next === "/" ? "/login" : `/login?next=${encodeURIComponent(next)}`);
23 }
24 return { next: nextPath(request) };
25}
26
27export async function action({ request }: Route.ActionArgs) {
28 assertSameOrigin(request);
29 const cookies = request.headers.get("cookie");
30 const challenge = readCookie(cookies, TWO_FACTOR_COOKIE);
31 if (!challenge) return { error: "This sign-in has expired. Sign in again.", expired: true };
32 const form = await request.formData();
33 const result = await identity.twoFactorSignIn(challenge, String(form.get("code") ?? ""), clientOf(request));
34 if (!result.ok) {
35 const expired = result.error.message.includes("expired");
36 return { error: result.error.message, expired };
37 }
38 const headers = new Headers({ "set-cookie": startSession(result.value.sessionToken) });
39 headers.append("set-cookie", cookie(TWO_FACTOR_COOKIE, "", 0));
40 // A GitHub sign-in waiting to be linked links now, as after a password.
41 const pending = readCookie(cookies, PENDING_COOKIE);
42 if (pending) {
43 await githubSignIn.claim(pending, result.value.user).catch(() => null);
44 headers.append("set-cookie", cookie(PENDING_COOKIE, "", 0));
45 }
46 throw redirect(nextPath(request), { headers });
47}
48
49export default function LoginTwoFactor({ actionData }: Route.ComponentProps) {
50 return (
51 <AuthCard
52 title="Two-factor authentication"
53 subtitle="Enter the code from your authenticator app"
54 footer={
55 <Link to="/login" className="text-fg underline underline-offset-4">
56 Sign in again
57 </Link>
58 }
59 >
60 <Form id="two-factor-sign-in" method="post" className="space-y-4">
61 {/* Sent as soon as the sixth digit is in; a recovery code pasted into the boxes moves to its own field. */}
62 <TwoFactorCodeField recovery autoFocus submit centered invalid={Boolean(actionData?.error)} />
63 <div className="text-center">
64 <ErrorText>{actionData?.error}</ErrorText>
65 </div>
66 <div className="pt-2 *:w-full">
67 <SubmitButton pending="Checking…">Verify</SubmitButton>
68 </div>
69 </Form>
70 </AuthCard>
71 );
72}