Skip to content
86 linesCodeBlameRaw
1import { Link, redirect } from "react-router";
2
3import type { Route } from "./+types/account";
4import { DeleteAccountAction } from "../../components/delete-account";
5import { DangerZone } from "../../components/danger-zone";
6import { ThemeSwitch } from "../../components/theme-switch";
7import { githubSignIn } from "../../lib/github.server";
8import { page } from "../../lib/meta";
9import { accounts } from "../../lib/services.server";
10import { assertSameOrigin, clientOf, endSession, requireUser, sessionTokenOf } from "../../lib/session.server";
11
12export function meta(args: Route.MetaArgs) {
13 return page(args, { title: "Account · Settings · g1t" });
14}
15
16export async function loader({ request, context }: Route.LoaderArgs) {
17 const user = requireUser(context, request);
18 // What deleting it would take, and anything in the way, shown before
19 // anyone types: workspaces you own alone, each with what billing needs.
20 const [deletion, github] = await Promise.all([
21 accounts.checkAccountDeletion(user).catch(() => null),
22 githubSignIn.account(user).catch(() => null),
23 ]);
24 return {
25 username: user.username,
26 deletion: deletion?.ok ? deletion.value : null,
27 hasPassword: github?.hasPassword ?? true,
28 };
29}
30
31/**
32 * Deleting the account: identity checks the person, the typed username,
33 * the proof that it is them, protection and the workspaces they own alone.
34 * Once it is deleted, this browser is signed out and sent home.
35 */
36export async function action({ request, context }: Route.ActionArgs) {
37 assertSameOrigin(request);
38 const user = requireUser(context, request);
39 const form = await request.formData();
40 if (form.get("intent") !== "delete-account") return null;
41 const password = String(form.get("password") ?? "");
42 const result = await accounts.deleteAccount(user, String(form.get("confirm") ?? ""), {
43 sessionToken: sessionTokenOf(request),
44 password: password || null,
45 client: clientOf(request),
46 });
47 if (!result.ok) return { deleteError: result.error.message, reauth: result.error.code === "reauth_required" };
48 throw redirect("/", { headers: { "set-cookie": await endSession(request) } });
49}
50
51export default function AccountSettings({ loaderData, actionData }: Route.ComponentProps) {
52 const { username, deletion, hasPassword } = loaderData;
53 return (
54 <div className="space-y-8">
55 <section aria-labelledby="username-heading">
56 <h2 id="username-heading" className="font-medium">
57 Username
58 </h2>
59 <p className="mt-1 text-sm text-muted">
60 You are <span className="font-mono text-fg">{username}</span>. Your profile is at{" "}
61 <Link to={`/${username}`} className="text-accent underline-offset-4 hover:underline">
62 g1t.sh/{username}
63 </Link>
64 .
65 </p>
66 </section>
67 <section aria-labelledby="appearance-heading">
68 <h2 id="appearance-heading" className="font-medium">
69 Appearance
70 </h2>
71 <p className="mt-1 text-sm text-muted">
72 Light, dark, or Auto to follow your system. This browser remembers it.
73 </p>
74 <ThemeSwitch className="mt-3 w-full max-w-xs" />
75 </section>
76 <DangerZone>
77 <DeleteAccountAction
78 username={username}
79 deletion={deletion}
80 hasPassword={hasPassword}
81 error={actionData?.deleteError}
82 />
83 </DangerZone>
84 </div>
85 );
86}