| 1 | import { Form, Link, redirect } from "react-router"; |
| 2 | |
| 3 | import type { Route } from "./+types/account"; |
| 4 | import { DeleteAccountAction } from "../../components/delete-account"; |
| 5 | import { DangerZone } from "../../components/danger-zone"; |
| 6 | import { DeveloperModeSwitch } from "../../components/developer-mode-switch"; |
| 7 | import { ThemeSwitch } from "../../components/theme-switch"; |
| 8 | import { ErrorText, SubmitButton } from "../../components/ui"; |
| 9 | import { githubSignIn } from "../../lib/github.server"; |
| 10 | import { page } from "../../lib/meta"; |
| 11 | import { accounts, privacy } from "../../lib/services.server"; |
| 12 | import { Unavailable } from "../../components/boundary"; |
| 13 | import { isFailed, part } from "../../lib/partial"; |
| 14 | import { assertSameOrigin, clientOf, endSession, requireUser, sessionTokenOf } from "../../lib/session.server"; |
| 15 | |
| 16 | export function meta(args: Route.MetaArgs) { |
| 17 | return page(args, { title: "Account · Settings · g1t" }); |
| 18 | } |
| 19 | |
| 20 | export async function loader({ request, context }: Route.LoaderArgs) { |
| 21 | const user = requireUser(context, request); |
| 22 | // What deleting it would take, and anything in the way, shown before |
| 23 | // anyone types: workspaces you own alone, each with what billing needs. |
| 24 | // Each section reads its own: one that could not be read says so in its |
| 25 | // place, and its form is not offered on what is not known. Whether they |
| 26 | // have a password only says whether deleting asks for one, and identity |
| 27 | // checks that anyway: asking for it is the safe guess. |
| 28 | const [deletion, github, erasure] = await Promise.all([ |
| 29 | part(accounts.checkAccountDeletion(user), "What deleting your account would take"), |
| 30 | githubSignIn.account(user).catch(() => null), |
| 31 | part(privacy.mine(user), "Your erasure request"), |
| 32 | ]); |
| 33 | return { |
| 34 | erasure, |
| 35 | username: user.username, |
| 36 | deletion: isFailed(deletion) ? deletion : deletion.ok ? deletion.value : null, |
| 37 | hasPassword: github?.hasPassword ?? true, |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Deleting the account: identity checks the person, the typed username, |
| 43 | * the proof that it is them, protection and the workspaces they own alone. |
| 44 | * Once it is deleted, this browser is signed out and sent home. |
| 45 | */ |
| 46 | export async function action({ request, context }: Route.ActionArgs) { |
| 47 | assertSameOrigin(request); |
| 48 | const user = requireUser(context, request); |
| 49 | const form = await request.formData(); |
| 50 | if (form.get("intent") === "request-erasure") { |
| 51 | // Identity unreachable is not a refusal: say so, and where else to ask. |
| 52 | const result = await privacy.request(user).catch(() => null); |
| 53 | if (!result) return { erasureError: "We could not record your request just now. Try again, or email support@g1t.sh." }; |
| 54 | return result.ok |
| 55 | ? { erasureNotice: `Request ${result.value.id} received. We will respond by ${result.value.due_at.slice(0, 10)}.` } |
| 56 | : { erasureError: result.error.message }; |
| 57 | } |
| 58 | if (form.get("intent") !== "delete-account") return null; |
| 59 | const password = String(form.get("password") ?? ""); |
| 60 | const result = await accounts.deleteAccount(user, String(form.get("confirm") ?? ""), { |
| 61 | sessionToken: sessionTokenOf(request), |
| 62 | password: password || null, |
| 63 | client: clientOf(request), |
| 64 | }); |
| 65 | if (!result.ok) return { deleteError: result.error.message, reauth: result.error.code === "reauth_required" }; |
| 66 | throw redirect("/", { headers: { "set-cookie": await endSession(request) } }); |
| 67 | } |
| 68 | |
| 69 | export default function AccountSettings({ loaderData, actionData }: Route.ComponentProps) { |
| 70 | const { username, deletion, hasPassword } = loaderData; |
| 71 | return ( |
| 72 | <div className="space-y-8"> |
| 73 | <section aria-labelledby="username-heading"> |
| 74 | <h2 id="username-heading" className="font-medium"> |
| 75 | Username |
| 76 | </h2> |
| 77 | <p className="mt-1 text-sm text-muted"> |
| 78 | You are <span className="font-mono text-fg">{username}</span>. Your profile is at{" "} |
| 79 | <Link to={`/${username}`} className="text-accent underline-offset-4 hover:underline"> |
| 80 | g1t.sh/{username} |
| 81 | </Link> |
| 82 | . |
| 83 | </p> |
| 84 | </section> |
| 85 | <section aria-labelledby="appearance-heading"> |
| 86 | <h2 id="appearance-heading" className="font-medium"> |
| 87 | Appearance |
| 88 | </h2> |
| 89 | <p className="mt-1 text-sm text-muted">Light, dark, or Auto to follow your system. This browser remembers it.</p> |
| 90 | <ThemeSwitch className="mt-3 w-full max-w-xs" /> |
| 91 | </section> |
| 92 | <section id="developer" aria-labelledby="developer-heading" className="scroll-mt-20"> |
| 93 | <h2 id="developer-heading" className="font-medium"> |
| 94 | Developer |
| 95 | </h2> |
| 96 | <p className="mt-1 text-sm text-muted">For reporting what went wrong when something breaks.</p> |
| 97 | <div className="mt-3"> |
| 98 | <DeveloperModeSwitch /> |
| 99 | </div> |
| 100 | </section> |
| 101 | <section aria-labelledby="erasure-heading" className="space-y-3"> |
| 102 | <h2 id="erasure-heading" className="font-medium"> |
| 103 | Erase my personal data |
| 104 | </h2> |
| 105 | <p className="text-sm text-muted"> |
| 106 | Request erasure even if workspace ownership or billing prevents account closure. We review shared content and any records we must retain, then explain |
| 107 | the outcome. This request does not immediately delete your account. |
| 108 | </p> |
| 109 | {isFailed(loaderData.erasure) ? ( |
| 110 | <Unavailable what="Your erasure request" /> |
| 111 | ) : loaderData.erasure ? ( |
| 112 | <p className="text-sm"> |
| 113 | Request {loaderData.erasure.id}: {loaderData.erasure.status}. Response due {loaderData.erasure.due_at.slice(0, 10)}. |
| 114 | </p> |
| 115 | ) : null} |
| 116 | {actionData?.erasureNotice && ( |
| 117 | <p role="status" className="text-sm"> |
| 118 | {actionData.erasureNotice} |
| 119 | </p> |
| 120 | )} |
| 121 | <ErrorText>{actionData?.erasureError}</ErrorText> |
| 122 | {!isFailed(loaderData.erasure) && (!loaderData.erasure || ["completed", "refused"].includes(loaderData.erasure.status)) && ( |
| 123 | <Form method="post"> |
| 124 | <SubmitButton name="intent" value="request-erasure" pending="Recording request..."> |
| 125 | Request erasure |
| 126 | </SubmitButton> |
| 127 | </Form> |
| 128 | )} |
| 129 | <p className="text-sm text-muted"> |
| 130 | For specific content, a request without signing in, or a question about an existing request, email{" "} |
| 131 | <a href="mailto:support@g1t.sh" className="underline"> |
| 132 | support@g1t.sh |
| 133 | </a> |
| 134 | . |
| 135 | </p> |
| 136 | </section> |
| 137 | <DangerZone> |
| 138 | {isFailed(deletion) ? ( |
| 139 | <Unavailable what="What deleting your account would take" /> |
| 140 | ) : ( |
| 141 | <DeleteAccountAction username={username} deletion={deletion} hasPassword={hasPassword} error={actionData?.deleteError} /> |
| 142 | )} |
| 143 | </DangerZone> |
| 144 | </div> |
| 145 | ); |
| 146 | } |