119 lines · 5.3 KBRaw
| 1 | import { Button, Input, LinkButton } from "@refs/design-system"; |
| 2 | import { MIN_PASSWORD_LENGTH } from "@refs/backend/contract"; |
| 3 | import { refsErrorCode, type PendingInvitation } from "@refs/backend/contract"; |
| 4 | import { Form, data, redirect, useNavigation } from "react-router"; |
| 5 | import type { Route } from "./+types/invite"; |
| 6 | import { AuthPage } from "../components/shell"; |
| 7 | import { failed, formText, found, gate, namespaceCookie, refs, sessionCookie } from "../lib/session"; |
| 8 | |
| 9 | async function pending(context: Route.LoaderArgs["context"], token: string): Promise<PendingInvitation | null> { |
| 10 | return gate(context) |
| 11 | .invitation(token) |
| 12 | .catch((error) => { |
| 13 | if (refsErrorCode(error) === "not_found") return null; |
| 14 | throw error; |
| 15 | }); |
| 16 | } |
| 17 | |
| 18 | export const meta: Route.MetaFunction = () => [{ title: "Join · refs" }]; |
| 19 | |
| 20 | export async function loader({ params, context }: Route.LoaderArgs) { |
| 21 | const invite = await pending(context, params.token); |
| 22 | const viewer = refs(context).viewer.person; |
| 23 | return data( |
| 24 | { org: invite?.org.name ?? "refs", email: invite?.email ?? null, existingAccount: invite?.existingAccount ?? false, viewer: viewer && { name: viewer.name, email: viewer.email }, token: params.token }, |
| 25 | { status: invite ? 200 : 404 }, |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | export async function action({ request, params, context }: Route.ActionArgs) { |
| 30 | const invite = await pending(context, params.token); |
| 31 | if (!invite) return data({ error: "This invitation is no longer valid.", name: "", handle: "" }, { status: 404 }); |
| 32 | |
| 33 | const form = await request.formData(); |
| 34 | if (formText(form, "intent") === "join") { |
| 35 | const joined = await found(refs(context).acceptInvitation(params.token)); |
| 36 | if (!joined.ok) return failed(joined.problem, { name: "", handle: "" }); |
| 37 | throw redirect(`/${joined.value.slug}`, { headers: { "Set-Cookie": namespaceCookie(joined.value.slug) } }); |
| 38 | } |
| 39 | const name = formText(form, "name"); |
| 40 | const handle = formText(form, "handle").toLowerCase(); |
| 41 | const outcome = await invite.accept({ name, handle, password: String(form.get("password") ?? "") }); |
| 42 | if (!outcome.ok) return failed(outcome.problem, { name, handle }); |
| 43 | throw redirect("/", { headers: { "Set-Cookie": sessionCookie(outcome.value) } }); |
| 44 | } |
| 45 | |
| 46 | export default function Invite({ loaderData, actionData }: Route.ComponentProps) { |
| 47 | const submitting = useNavigation().state === "submitting"; |
| 48 | if (!loaderData.email) { |
| 49 | return ( |
| 50 | <AuthPage subtitle={`Join ${loaderData.org}`} error="This invitation is invalid, has expired, or was already used. Ask an admin for a new one."> |
| 51 | <LinkButton href="/login" variant="primary" size="lg" className="w-full justify-center"> |
| 52 | Go to sign in |
| 53 | </LinkButton> |
| 54 | </AuthPage> |
| 55 | ); |
| 56 | } |
| 57 | if (loaderData.viewer) { |
| 58 | const matches = loaderData.viewer.email.toLowerCase() === loaderData.email.toLowerCase(); |
| 59 | return ( |
| 60 | <AuthPage subtitle={`You've been invited to join ${loaderData.org}.`} error={actionData?.error}> |
| 61 | {matches ? ( |
| 62 | <Form method="post" className="flex flex-col gap-4"> |
| 63 | <input type="hidden" name="intent" value="join" /> |
| 64 | <p className="m-0"> |
| 65 | Join as <strong>{loaderData.viewer.name}</strong> ({loaderData.viewer.email}). |
| 66 | </p> |
| 67 | <Button type="submit" variant="primary" size="lg" className="w-full justify-center" loading={submitting}> |
| 68 | Join {loaderData.org} |
| 69 | </Button> |
| 70 | </Form> |
| 71 | ) : ( |
| 72 | <p className="m-0 text-pretty"> |
| 73 | This invitation is for {loaderData.email}, and you're signed in as {loaderData.viewer.email}. Sign out, then sign in as {loaderData.email} to accept it. |
| 74 | </p> |
| 75 | )} |
| 76 | </AuthPage> |
| 77 | ); |
| 78 | } |
| 79 | if (loaderData.existingAccount) { |
| 80 | return ( |
| 81 | <AuthPage subtitle={`You've been invited to join ${loaderData.org}.`}> |
| 82 | <p className="m-0 text-pretty">{loaderData.email} already has an account. Sign in to accept the invitation.</p> |
| 83 | <LinkButton href={`/login?next=${encodeURIComponent(`/invite/${loaderData.token}`)}`} variant="primary" size="lg" className="mt-5 w-full justify-center"> |
| 84 | Sign in to accept |
| 85 | </LinkButton> |
| 86 | </AuthPage> |
| 87 | ); |
| 88 | } |
| 89 | return ( |
| 90 | <AuthPage subtitle={`You've been invited to join ${loaderData.org}.`} error={actionData?.error}> |
| 91 | <Form key={JSON.stringify(actionData ?? null)} method="post" className="flex flex-col gap-4"> |
| 92 | <Input label="Email" name="email" type="email" value={loaderData.email} readOnly disabled /> |
| 93 | <Input label="Your name" name="name" defaultValue={actionData?.name} maxLength={64} required autoFocus /> |
| 94 | <Input |
| 95 | label="Username" |
| 96 | name="handle" |
| 97 | defaultValue={actionData?.handle} |
| 98 | maxLength={39} |
| 99 | pattern="[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,37}[a-zA-Z0-9])?" |
| 100 | autoComplete="username" |
| 101 | description="Your personal namespace: your own repositories live at /username/repo." |
| 102 | required |
| 103 | /> |
| 104 | <Input |
| 105 | label="Password" |
| 106 | name="password" |
| 107 | type="password" |
| 108 | minLength={MIN_PASSWORD_LENGTH} |
| 109 | autoComplete="new-password" |
| 110 | description={`At least ${MIN_PASSWORD_LENGTH} characters.`} |
| 111 | required |
| 112 | /> |
| 113 | <Button type="submit" variant="primary" size="lg" className="w-full justify-center" loading={submitting}> |
| 114 | Join {loaderData.org} |
| 115 | </Button> |
| 116 | </Form> |
| 117 | </AuthPage> |
| 118 | ); |
| 119 | } |