63 lines · 2.8 KBRaw
| 1 | import { Button, Input } from "@refs/design-system"; |
| 2 | import { MIN_PASSWORD_LENGTH } from "@refs/backend/contract"; |
| 3 | import { Form, Link, redirect, useNavigation, useSearchParams } from "react-router"; |
| 4 | import type { Route } from "./+types/signup"; |
| 5 | import { AuthPage } from "../components/shell"; |
| 6 | import { failed, formText, gate, refs, sessionCookie } from "../lib/session"; |
| 7 | |
| 8 | export const meta: Route.MetaFunction = () => [{ title: "Create your account · refs" }]; |
| 9 | |
| 10 | export function loader({ context }: Route.LoaderArgs) { |
| 11 | if (refs(context).viewer.person) throw redirect("/"); |
| 12 | return null; |
| 13 | } |
| 14 | |
| 15 | export async function action({ request, context }: Route.ActionArgs) { |
| 16 | const form = await request.formData(); |
| 17 | const values = { name: formText(form, "name"), handle: formText(form, "handle").toLowerCase(), email: formText(form, "email").toLowerCase() }; |
| 18 | const outcome = await gate(context).signUp({ ...values, password: String(form.get("password") ?? ""), ip: request.headers.get("cf-connecting-ip") ?? "unknown" }); |
| 19 | if (!outcome.ok) return failed(outcome.problem, { values }); |
| 20 | throw redirect(`/${values.handle}`, { headers: { "Set-Cookie": sessionCookie(outcome.value) } }); |
| 21 | } |
| 22 | |
| 23 | export default function Signup({ actionData }: Route.ComponentProps) { |
| 24 | const submitting = useNavigation().state === "submitting"; |
| 25 | const [search] = useSearchParams(); |
| 26 | const values = actionData?.values; |
| 27 | return ( |
| 28 | <AuthPage subtitle="Create your account" error={actionData?.error}> |
| 29 | <Form key={JSON.stringify(actionData ?? null)} method="post" className="flex flex-col gap-4"> |
| 30 | <Input label="Your name" name="name" defaultValue={values?.name} maxLength={64} autoComplete="name" required autoFocus /> |
| 31 | <Input |
| 32 | label="Username" |
| 33 | name="handle" |
| 34 | defaultValue={values?.handle} |
| 35 | maxLength={39} |
| 36 | pattern="[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,37}[a-zA-Z0-9])?" |
| 37 | autoComplete="username" |
| 38 | description="Your personal namespace: your repositories live at /username/repo." |
| 39 | required |
| 40 | /> |
| 41 | <Input label="Email" name="email" type="email" defaultValue={values?.email ?? search.get("email") ?? undefined} autoComplete="email" required /> |
| 42 | <Input |
| 43 | label="Password" |
| 44 | name="password" |
| 45 | type="password" |
| 46 | minLength={MIN_PASSWORD_LENGTH} |
| 47 | autoComplete="new-password" |
| 48 | description={`At least ${MIN_PASSWORD_LENGTH} characters.`} |
| 49 | required |
| 50 | /> |
| 51 | <Button type="submit" variant="primary" size="lg" className="w-full justify-center" loading={submitting}> |
| 52 | Create account |
| 53 | </Button> |
| 54 | <p className="m-0 text-center text-sm text-muted"> |
| 55 | Have an account?{" "} |
| 56 | <Link to="/login" className="text-accent-text"> |
| 57 | Sign in |
| 58 | </Link> |
| 59 | </p> |
| 60 | </Form> |
| 61 | </AuthPage> |
| 62 | ); |
| 63 | } |