| 1 | import { CircleCheck, MailWarning } from "lucide-react"; |
| 2 | import { redirect } from "react-router"; |
| 3 | |
| 4 | import type { Route } from "./+types/verify"; |
| 5 | import { page } from "../lib/meta"; |
| 6 | import { ButtonLink } from "../components/ui"; |
| 7 | import { identity } from "../lib/services.server"; |
| 8 | import { assertSameOrigin, getViewer, requireUser } from "../lib/session.server"; |
| 9 | |
| 10 | export function meta(args: Route.MetaArgs) { |
| 11 | return page(args, { title: "Confirm your email · g1t" }); |
| 12 | } |
| 13 | |
| 14 | /** Follows the link from the confirmation email. */ |
| 15 | export async function loader({ request, context }: Route.LoaderArgs) { |
| 16 | const token = new URL(request.url).searchParams.get("token") ?? ""; |
| 17 | const result = await identity.verifyEmail(token); |
| 18 | // An address added to an account already in use goes back to settings; |
| 19 | // a new account's first goes on to make a workspace. |
| 20 | const settled = (getViewer(context)?.workspaces ?? []).length > 0; |
| 21 | return result.ok |
| 22 | ? { ok: true as const, username: result.value.username, added: settled } |
| 23 | : { ok: false as const, message: result.error.message }; |
| 24 | } |
| 25 | |
| 26 | /** "Resend" from the banner shown to unconfirmed accounts. */ |
| 27 | export async function action({ request, context }: Route.ActionArgs) { |
| 28 | assertSameOrigin(request); |
| 29 | await identity.resendVerification(requireUser(context, request)); |
| 30 | throw redirect("/?sent=1"); |
| 31 | } |
| 32 | |
| 33 | export default function Verify({ loaderData }: Route.ComponentProps) { |
| 34 | return ( |
| 35 | <main className="mx-auto max-w-md px-4 py-32 text-center"> |
| 36 | {loaderData.ok ? ( |
| 37 | <> |
| 38 | <CircleCheck size={40} className="mx-auto text-accent" /> |
| 39 | <h1 className="mt-6 text-2xl font-semibold tracking-tight"> |
| 40 | Email confirmed |
| 41 | </h1> |
| 42 | {loaderData.added ? ( |
| 43 | <> |
| 44 | <p className="mt-2 text-muted"> |
| 45 | The address is confirmed on {loaderData.username}. You can make it |
| 46 | your primary address in your settings. |
| 47 | </p> |
| 48 | <div className="mt-8"> |
| 49 | <ButtonLink to="/settings/emails">Back to settings</ButtonLink> |
| 50 | </div> |
| 51 | </> |
| 52 | ) : ( |
| 53 | <> |
| 54 | <p className="mt-2 text-muted"> |
| 55 | Your account {loaderData.username} is ready. Next, create the |
| 56 | workspace your repositories will live in. |
| 57 | </p> |
| 58 | <div className="mt-8"> |
| 59 | <ButtonLink to="/">Continue</ButtonLink> |
| 60 | </div> |
| 61 | </> |
| 62 | )} |
| 63 | </> |
| 64 | ) : ( |
| 65 | <> |
| 66 | <MailWarning size={40} className="mx-auto text-warn" /> |
| 67 | <h1 className="mt-6 text-2xl font-semibold tracking-tight"> |
| 68 | That link did not work |
| 69 | </h1> |
| 70 | <p className="mt-2 text-muted">{loaderData.message}</p> |
| 71 | <p className="mt-2 text-sm text-muted"> |
| 72 | Sign in and use the banner at the top to send a new one. |
| 73 | </p> |
| 74 | <div className="mt-8"> |
| 75 | <ButtonLink to="/login" variant="quiet"> |
| 76 | Sign in |
| 77 | </ButtonLink> |
| 78 | </div> |
| 79 | </> |
| 80 | )} |
| 81 | </main> |
| 82 | ); |
| 83 | } |