| 1 | import { CheckCircle2, Ticket } from "lucide-react"; |
| 2 | import { Form, Link, data, redirect } from "react-router"; |
| 3 | |
| 4 | import type { InvitePreview } from "@g1t/contracts"; |
| 5 | |
| 6 | import type { Route } from "./+types/register"; |
| 7 | import { page } from "../lib/meta"; |
| 8 | import { AuthCard } from "../components/auth-card"; |
| 9 | import { ContinueWithGithub, OrDivider } from "../components/github"; |
| 10 | import { Honeypot } from "../components/honeypot"; |
| 11 | import { githubSignInEnabled } from "../lib/github.server"; |
| 12 | import { Avatar, Button, ErrorText, Field, Input } from "../components/ui"; |
| 13 | import { identity } from "../lib/services.server"; |
| 14 | import { cleanCode, looksAutomated } from "../lib/invites"; |
| 15 | import { clientKey, registrationMode } from "../lib/registration.server"; |
| 16 | import { |
| 17 | assertSameOrigin, |
| 18 | getViewer, |
| 19 | nextPath, |
| 20 | startSession, |
| 21 | } from "../lib/session.server"; |
| 22 | |
| 23 | export function meta(args: Route.MetaArgs) { |
| 24 | return page(args, { title: "Create an account · g1t" }); |
| 25 | } |
| 26 | |
| 27 | export async function loader({ request, context }: Route.LoaderArgs) { |
| 28 | if (getViewer(context)) throw redirect(nextPath(request)); |
| 29 | const inviteOnly = (await registrationMode()) !== "open"; |
| 30 | const code = cleanCode(new URL(request.url).searchParams.get("invite")); |
| 31 | // The invite is checked before anything else on the page: without a good |
| 32 | // one, there is no form to fill in. |
| 33 | const checked = code ? await identity.checkInvite(code, clientKey(request)) : null; |
| 34 | const invite: InvitePreview | null = checked?.ok ? checked.value : null; |
| 35 | // A good invite is used on its own page, which knows whom it is from and |
| 36 | // where it leads; it signs up, joins and lands in one go. |
| 37 | if (invite && code) throw redirect(`/invite/${encodeURIComponent(code)}`); |
| 38 | // Signing up with GitHub carries the invite code and `next` through it. |
| 39 | const params = new URLSearchParams(); |
| 40 | if (code) params.set("invite", code); |
| 41 | if (nextPath(request) !== "/") params.set("next", nextPath(request)); |
| 42 | const query = params.toString(); |
| 43 | return { |
| 44 | inviteOnly, |
| 45 | code, |
| 46 | invite, |
| 47 | inviteError: checked && !checked.ok ? checked.error.message : null, |
| 48 | github: await githubSignInEnabled(), |
| 49 | githubUrl: query ? `/auth/github?${query}` : "/auth/github", |
| 50 | started: Date.now(), |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | export async function action({ request }: Route.ActionArgs) { |
| 55 | assertSameOrigin(request); |
| 56 | const form = await request.formData(); |
| 57 | const client = clientKey(request); |
| 58 | |
| 59 | if (form.get("intent") === "request-access") { |
| 60 | // A bot gets the same thanks as a person, and nothing is kept. |
| 61 | if (looksAutomated(form)) return { requested: true }; |
| 62 | const result = await identity.requestAccess( |
| 63 | String(form.get("email") ?? ""), |
| 64 | String(form.get("about") ?? ""), |
| 65 | client, |
| 66 | ); |
| 67 | if (!result.ok) return data({ requestError: result.error.message }, { status: 422 }); |
| 68 | return { requested: true }; |
| 69 | } |
| 70 | |
| 71 | const code = cleanCode(String(form.get("invite") ?? "")); |
| 72 | if (looksAutomated(form)) return data({ error: "Something went wrong. Try again." }, { status: 422 }); |
| 73 | // The invite first, so its problem is the one shown. |
| 74 | let joins: string | null = null; |
| 75 | if (code) { |
| 76 | const checked = await identity.checkInvite(code, client); |
| 77 | if (!checked.ok) return data({ error: checked.error.message }, { status: 422 }); |
| 78 | joins = checked.value.workspace?.slug ?? null; |
| 79 | } |
| 80 | const result = await identity.register( |
| 81 | String(form.get("username") ?? ""), |
| 82 | String(form.get("email") ?? ""), |
| 83 | String(form.get("password") ?? ""), |
| 84 | code || null, |
| 85 | client, |
| 86 | ); |
| 87 | if (!result.ok) return data({ error: result.error.message }, { status: 422 }); |
| 88 | // An invite to a workspace lands in it; anything else goes where it was going. |
| 89 | const next = nextPath(request); |
| 90 | throw redirect(joins && next === "/" ? `/${joins}` : next, { |
| 91 | headers: { "set-cookie": startSession(result.value.sessionToken) }, |
| 92 | }); |
| 93 | } |
| 94 | |
| 95 | /** Who sent the invite and what it joins, above the form. */ |
| 96 | function InvitedBy({ invite }: { invite: InvitePreview }) { |
| 97 | const from = invite.invitedBy; |
| 98 | return ( |
| 99 | <div className="mb-6 flex items-center gap-3 rounded-lg border border-accent/30 bg-accent/5 p-3" role="status"> |
| 100 | {invite.workspace ? ( |
| 101 | <Avatar name={invite.workspace.slug} image={invite.workspace.avatar} size={36} square /> |
| 102 | ) : from ? ( |
| 103 | <Avatar name={from.username} image={from.avatar} size={36} /> |
| 104 | ) : ( |
| 105 | <span className="inline-flex size-9 shrink-0 items-center justify-center rounded-full bg-accent/15 text-accent"> |
| 106 | <Ticket size={18} /> |
| 107 | </span> |
| 108 | )} |
| 109 | <p className="min-w-0 text-sm leading-5"> |
| 110 | {from ? ( |
| 111 | <> |
| 112 | <span className="font-medium text-fg">{from.name ?? from.username}</span> |
| 113 | {from.name && <span className="font-mono text-muted"> @{from.username}</span>} |
| 114 | </> |
| 115 | ) : ( |
| 116 | <span className="font-medium text-fg">The g1t team</span> |
| 117 | )}{" "} |
| 118 | <span className="text-muted"> |
| 119 | invited you |
| 120 | {invite.workspace ? ( |
| 121 | <> |
| 122 | {" "}to <span className="font-medium text-fg">{invite.workspace.name}</span> |
| 123 | </> |
| 124 | ) : ( |
| 125 | " to g1t" |
| 126 | )} |
| 127 | . |
| 128 | </span> |
| 129 | </p> |
| 130 | </div> |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | function SignUpForm({ |
| 135 | loaderData, |
| 136 | error, |
| 137 | }: { |
| 138 | loaderData: Route.ComponentProps["loaderData"]; |
| 139 | error: string | undefined; |
| 140 | }) { |
| 141 | const { invite, code, inviteOnly } = loaderData; |
| 142 | return ( |
| 143 | <> |
| 144 | {invite && <InvitedBy invite={invite} />} |
| 145 | {loaderData.github && ( |
| 146 | <> |
| 147 | <ContinueWithGithub href={loaderData.githubUrl} /> |
| 148 | <OrDivider /> |
| 149 | </> |
| 150 | )} |
| 151 | <Form method="post" className="relative space-y-4"> |
| 152 | <Honeypot started={loaderData.started} /> |
| 153 | {inviteOnly || code ? ( |
| 154 | <Field label="Invite code" hint={invite ? undefined : "From your invite link or email."}> |
| 155 | <span className="block font-mono"> |
| 156 | <Input name="invite" defaultValue={code} required={inviteOnly} spellCheck={false} autoCapitalize="none" /> |
| 157 | </span> |
| 158 | </Field> |
| 159 | ) : null} |
| 160 | <Field |
| 161 | label="Username" |
| 162 | hint="Lowercase letters, digits and hyphens. It is how you sign in and how others see you." |
| 163 | > |
| 164 | <Input |
| 165 | name="username" |
| 166 | autoComplete="username" |
| 167 | required |
| 168 | autoFocus |
| 169 | maxLength={39} |
| 170 | pattern="[a-z0-9](?:[a-z0-9]|-(?=[a-z0-9]))*" |
| 171 | /> |
| 172 | </Field> |
| 173 | <Field |
| 174 | label="Email" |
| 175 | hint={invite?.email ? `This invite is for ${invite.email}. Use that address.` : undefined} |
| 176 | > |
| 177 | <Input name="email" type="email" autoComplete="email" required /> |
| 178 | </Field> |
| 179 | <Field label="Password" hint="At least 10 characters."> |
| 180 | <Input |
| 181 | name="password" |
| 182 | type="password" |
| 183 | autoComplete="new-password" |
| 184 | required |
| 185 | minLength={10} |
| 186 | /> |
| 187 | </Field> |
| 188 | <ErrorText>{error}</ErrorText> |
| 189 | <div className="pt-2 *:w-full"> |
| 190 | <Button type="submit">{invite?.workspace ? `Create account and join ${invite.workspace.slug}` : "Create account"}</Button> |
| 191 | </div> |
| 192 | </Form> |
| 193 | </> |
| 194 | ); |
| 195 | } |
| 196 | |
| 197 | /** Invite-only and no good code: enter one, or ask for access. */ |
| 198 | function InviteOnly({ |
| 199 | loaderData, |
| 200 | actionData, |
| 201 | }: { |
| 202 | loaderData: Route.ComponentProps["loaderData"]; |
| 203 | actionData: Route.ComponentProps["actionData"]; |
| 204 | }) { |
| 205 | const requested = actionData && "requested" in actionData; |
| 206 | return ( |
| 207 | <div className="space-y-8"> |
| 208 | <section id="invite" className="scroll-mt-24"> |
| 209 | <h2 className="text-sm font-medium">Have an invite?</h2> |
| 210 | <p className="mt-1 text-sm text-muted">Paste the code or link from your invite.</p> |
| 211 | <Form method="get" className="mt-3 flex gap-2"> |
| 212 | <div className="min-w-0 grow font-mono"> |
| 213 | <Input |
| 214 | name="invite" |
| 215 | aria-label="Invite code" |
| 216 | placeholder="g1t-xxxx-xxxx-…" |
| 217 | defaultValue={loaderData.code} |
| 218 | required |
| 219 | spellCheck={false} |
| 220 | autoCapitalize="none" |
| 221 | /> |
| 222 | </div> |
| 223 | <Button type="submit" variant="quiet"> |
| 224 | Continue |
| 225 | </Button> |
| 226 | </Form> |
| 227 | {loaderData.inviteError && ( |
| 228 | <div className="mt-2"> |
| 229 | <ErrorText>{loaderData.inviteError}</ErrorText> |
| 230 | </div> |
| 231 | )} |
| 232 | </section> |
| 233 | |
| 234 | <section id="request" className="scroll-mt-24 border-t border-line pt-8"> |
| 235 | <h2 className="text-sm font-medium">Request access</h2> |
| 236 | {requested ? ( |
| 237 | <p className="mt-3 flex items-start gap-2 rounded-md border border-accent/40 bg-surface p-3 text-sm" role="status"> |
| 238 | <CheckCircle2 size={16} className="mt-0.5 shrink-0 text-accent" /> |
| 239 | <span> |
| 240 | You are on the list, and your invite comes to that address when a place opens. The first time an address |
| 241 | asks, we email it to confirm. |
| 242 | </span> |
| 243 | </p> |
| 244 | ) : ( |
| 245 | <> |
| 246 | <p className="mt-1 text-sm text-muted"> |
| 247 | Leave your email and we will send an invite as we open up. Someone already on g1t can also invite you. |
| 248 | </p> |
| 249 | <Form method="post" className="relative mt-3 space-y-3"> |
| 250 | <input type="hidden" name="intent" value="request-access" /> |
| 251 | <Honeypot started={loaderData.started} /> |
| 252 | <Field label="Email"> |
| 253 | <Input name="email" type="email" autoComplete="email" required maxLength={254} /> |
| 254 | </Field> |
| 255 | <Field label="What will you build? (optional)"> |
| 256 | <textarea |
| 257 | name="about" |
| 258 | rows={3} |
| 259 | maxLength={1000} |
| 260 | className="w-full rounded-md border border-line bg-bg px-3 py-2 text-sm outline-none transition-colors placeholder:text-faint hover:border-line-strong focus:border-accent-dim" |
| 261 | /> |
| 262 | </Field> |
| 263 | <ErrorText>{actionData && "requestError" in actionData ? actionData.requestError : undefined}</ErrorText> |
| 264 | <div className="*:w-full"> |
| 265 | <Button type="submit">Request access</Button> |
| 266 | </div> |
| 267 | </Form> |
| 268 | </> |
| 269 | )} |
| 270 | </section> |
| 271 | </div> |
| 272 | ); |
| 273 | } |
| 274 | |
| 275 | export default function Register({ loaderData, actionData }: Route.ComponentProps) { |
| 276 | const { inviteOnly, invite } = loaderData; |
| 277 | const error = actionData && "error" in actionData ? actionData.error : undefined; |
| 278 | // Invite-only: the sign-up form only appears with a good code (or a |
| 279 | // submitted one that failed, so the person can correct the rest). |
| 280 | const showForm = !inviteOnly || invite !== null; |
| 281 | return ( |
| 282 | <AuthCard |
| 283 | title={showForm ? "Create your account" : "g1t is invite-only for now"} |
| 284 | subtitle={showForm ? "Where people and agents ship software together" : "Enter your invite, or ask for one"} |
| 285 | footer={ |
| 286 | <> |
| 287 | Already have an account?{" "} |
| 288 | <Link to="/login" className="text-fg underline underline-offset-4"> |
| 289 | Sign in |
| 290 | </Link> |
| 291 | </> |
| 292 | } |
| 293 | > |
| 294 | {showForm ? <SignUpForm loaderData={loaderData} error={error} /> : <InviteOnly loaderData={loaderData} actionData={actionData} />} |
| 295 | </AuthCard> |
| 296 | ); |
| 297 | } |