78 lines · 4.1 KBRaw
| 1 | import { AgentAvatar, Avatar, Button } from "@refs/design-system"; |
| 2 | import { data, Form, redirect, useNavigation } from "react-router"; |
| 3 | import type { Route } from "./+types/oauth-authorize"; |
| 4 | import { refsErrorCode } from "@refs/backend/contract"; |
| 5 | import { AuthPage } from "../components/shell"; |
| 6 | import { requestContext } from "../context"; |
| 7 | import { formText } from "../lib/session"; |
| 8 | |
| 9 | export const meta: Route.MetaFunction = () => [{ title: "Connect an agent · refs" }]; |
| 10 | |
| 11 | export function headers({ loaderHeaders, actionHeaders }: Route.HeadersArgs) { |
| 12 | return actionHeaders.has("set-cookie") ? actionHeaders : loaderHeaders; |
| 13 | } |
| 14 | |
| 15 | export async function loader({ request, context }: Route.LoaderArgs) { |
| 16 | const url = new URL(request.url); |
| 17 | let step; |
| 18 | try { |
| 19 | step = await context.get(requestContext).backend.agentConsent(request).begin(); |
| 20 | } catch (error) { |
| 21 | if (refsErrorCode(error) === "sign_in_required") throw redirect(`/login?next=${encodeURIComponent(url.pathname + url.search)}`); |
| 22 | throw error; |
| 23 | } |
| 24 | if (!step.ok) return data({ view: null, error: step.problem.message }, { status: 400 }); |
| 25 | if ("redirectTo" in step.value) throw redirect(step.value.redirectTo, { headers: step.value.headers }); |
| 26 | return data({ view: step.value.view, error: null }, { headers: step.value.headers }); |
| 27 | } |
| 28 | |
| 29 | export async function action({ request, context }: Route.ActionArgs) { |
| 30 | const form = await request.formData(); |
| 31 | const consent = context.get(requestContext).backend.agentConsent(request); |
| 32 | const handle = formText(form, "handle"); |
| 33 | const step = formText(form, "intent") === "allow" ? await consent.approve(handle) : await consent.deny(handle); |
| 34 | if (!step.ok) return { error: step.problem.message }; |
| 35 | throw redirect(step.value.redirectTo, { headers: step.value.headers }); |
| 36 | } |
| 37 | |
| 38 | export default function Authorize({ loaderData, actionData }: Route.ComponentProps) { |
| 39 | const navigation = useNavigation(); |
| 40 | const intent = navigation.state === "submitting" ? navigation.formData?.get("intent") : null; |
| 41 | const view = loaderData.view; |
| 42 | const error = actionData?.error ?? loaderData.error ?? undefined; |
| 43 | if (!view) return <AuthPage subtitle="Connect an agent" error={error ?? "This sign-in link is not valid. Start again from your agent."}>{null}</AuthPage>; |
| 44 | return ( |
| 45 | <AuthPage subtitle="Connect an agent" error={error}> |
| 46 | <Form method="post" className="flex flex-col gap-5"> |
| 47 | <input type="hidden" name="handle" value={view.handle} /> |
| 48 | <div className="flex items-center gap-3"> |
| 49 | <AgentAvatar name={view.client} size="base" /> |
| 50 | <p className="m-0 leading-snug"> |
| 51 | <span className="font-semibold">{view.client}</span> wants to act as{" "} |
| 52 | <span className="inline-flex items-center gap-1 align-middle font-semibold"><Avatar name={view.person.name} />{view.person.name}</span> on refs. |
| 53 | </p> |
| 54 | </div> |
| 55 | <div className="flex flex-col gap-2"> |
| 56 | <span className="caption text-faint">It will be able to</span> |
| 57 | <ul className="m-0 flex list-none flex-col divide-y divide-hairline rounded-control bg-sunken p-0 text-compact shadow-[inset_0_0_0_1px_var(--hairline)]"> |
| 58 | <li className="px-3 py-2">Come online on repositories you can push to</li> |
| 59 | <li className="px-3 py-2">Clone and read those repositories</li> |
| 60 | <li className="px-3 py-2">Take a branch and push to it</li> |
| 61 | </ul> |
| 62 | </div> |
| 63 | <p className="m-0 text-sm text-muted"> |
| 64 | You'll be sent back to <span className="font-mono">{view.redirectHost}</span> |
| 65 | {view.loopback ? ", an app on this computer" : ""}. You can disconnect it any time under Access tokens. |
| 66 | </p> |
| 67 | <div className="flex gap-3"> |
| 68 | <Button type="submit" name="intent" value="allow" variant="primary" size="lg" className="flex-1 justify-center" loading={intent === "allow"}> |
| 69 | Allow |
| 70 | </Button> |
| 71 | <Button type="submit" name="intent" value="deny" variant="secondary" size="lg" className="flex-1 justify-center" loading={intent === "deny"}> |
| 72 | Deny |
| 73 | </Button> |
| 74 | </div> |
| 75 | </Form> |
| 76 | </AuthPage> |
| 77 | ); |
| 78 | } |