g1t/apps/web/app/routes/device.tsx
| 1 | import { CircleCheck, CircleX, KeyRound } from "lucide-react"; |
| 2 | import { Form } from "react-router"; |
| 3 | |
| 4 | import type { Route } from "./+types/device"; |
| 5 | import { Button, ErrorText, Field, Input } from "../components/ui"; |
| 6 | import { identity } from "../lib/services.server"; |
| 7 | import { assertSameOrigin, requireUser } from "../lib/session.server"; |
| 8 | |
| 9 | export function meta({}: Route.MetaArgs) { |
| 10 | return [{ title: "Connect an application · g1t" }]; |
| 11 | } |
| 12 | |
| 13 | /** Where a tool sends a person to approve its sign-in. */ |
| 14 | export async function loader({ request, context }: Route.LoaderArgs) { |
| 15 | const user = requireUser(context, request); |
| 16 | const code = new URL(request.url).searchParams.get("code") ?? ""; |
| 17 | return { |
| 18 | user, |
| 19 | code, |
| 20 | pending: code ? await identity.deviceLookup(code) : null, |
| 21 | }; |
| 22 | } |
| 23 | |
| 24 | export async function action({ request, context }: Route.ActionArgs) { |
| 25 | assertSameOrigin(request); |
| 26 | const user = requireUser(context, request); |
| 27 | const form = await request.formData(); |
| 28 | const approve = form.get("decision") === "approve"; |
| 29 | const result = await identity.deviceResolve( |
| 30 | String(form.get("code") ?? ""), |
| 31 | user, |
| 32 | approve, |
| 33 | ); |
| 34 | return result.ok |
| 35 | ? { done: approve ? ("approved" as const) : ("denied" as const) } |
| 36 | : { error: result.error.message }; |
| 37 | } |
| 38 | |
| 39 | export default function Device({ loaderData, actionData }: Route.ComponentProps) { |
| 40 | const { user, code, pending } = loaderData; |
| 41 | |
| 42 | if (actionData && "done" in actionData) { |
| 43 | const approved = actionData.done === "approved"; |
| 44 | const Icon = approved ? CircleCheck : CircleX; |
| 45 | return ( |
| 46 | <main className="mx-auto max-w-md px-4 py-32 text-center"> |
| 47 | <Icon size={40} className={`mx-auto ${approved ? "text-accent" : "text-muted"}`} /> |
| 48 | <h1 className="mt-6 text-2xl font-semibold tracking-tight"> |
| 49 | {approved ? "Connected" : "Request denied"} |
| 50 | </h1> |
| 51 | <p className="mt-2 text-muted"> |
| 52 | {approved |
| 53 | ? "Go back to the application. It will finish signing in on its own." |
| 54 | : "Nothing was given access to your account."} |
| 55 | </p> |
| 56 | </main> |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | return ( |
| 61 | <main className="mx-auto max-w-md px-4 py-24"> |
| 62 | <KeyRound size={36} className="text-accent" /> |
| 63 | <h1 className="mt-6 text-2xl font-semibold tracking-tight"> |
| 64 | Connect an application |
| 65 | </h1> |
| 66 | |
| 67 | {pending ? ( |
| 68 | <> |
| 69 | <p className="mt-2 text-muted"> |
| 70 | <span className="font-medium text-fg">{pending.clientName}</span>{" "} |
| 71 | wants to act as{" "} |
| 72 | <span className="font-mono font-medium text-fg">{user.username}</span>{" "} |
| 73 | on g1t. |
| 74 | </p> |
| 75 | <p className="mt-6 text-sm text-muted"> |
| 76 | Check that this code matches the one it is showing you: |
| 77 | </p> |
| 78 | <p className="mt-2 rounded-xl border border-line bg-surface py-5 text-center font-mono text-3xl font-semibold tracking-[0.2em]"> |
| 79 | {pending.userCode} |
| 80 | </p> |
| 81 | <p className="mt-4 text-sm text-muted"> |
| 82 | Approving creates an access token with the full rights of your |
| 83 | account. You can delete it in settings at any time. |
| 84 | </p> |
| 85 | <Form method="post" className="mt-6 flex gap-3"> |
| 86 | <input type="hidden" name="code" value={pending.userCode} /> |
| 87 | <div className="grow *:w-full"> |
| 88 | <Button variant="accent" type="submit" name="decision" value="approve"> |
| 89 | Approve |
| 90 | </Button> |
| 91 | </div> |
| 92 | <Button variant="quiet" type="submit" name="decision" value="deny"> |
| 93 | Deny |
| 94 | </Button> |
| 95 | </Form> |
| 96 | </> |
| 97 | ) : ( |
| 98 | <> |
| 99 | <p className="mt-2 text-muted"> |
| 100 | {code |
| 101 | ? "That code is not valid or has expired. Start again from the application, or enter the code it shows." |
| 102 | : "Enter the code the application is showing you."} |
| 103 | </p> |
| 104 | <Form method="get" className="mt-6 space-y-4"> |
| 105 | <Field label="Code"> |
| 106 | <Input |
| 107 | name="code" |
| 108 | placeholder="XXXX-XXXX" |
| 109 | autoComplete="off" |
| 110 | autoCapitalize="characters" |
| 111 | required |
| 112 | autoFocus |
| 113 | /> |
| 114 | </Field> |
| 115 | <div className="*:w-full"> |
| 116 | <Button type="submit">Continue</Button> |
| 117 | </div> |
| 118 | </Form> |
| 119 | </> |
| 120 | )} |
| 121 | <ErrorText>{actionData && "error" in actionData ? actionData.error : null}</ErrorText> |
| 122 | </main> |
| 123 | ); |
| 124 | } |