Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
π Delete accounts immediately on self-hosted instances (#2805)
Luke Vellacd8b4e26795b
diff --git a/apps/web/public/locales/en/app.json b/apps/web/public/locales/en/app.jsonindex e2953766d..6713656ca 100644--- a/apps/web/public/locales/en/app.json+++ b/apps/web/public/locales/en/app.json@@ -196,8 +196,11 @@"deleteAccount": "Delete Account","deleteAccountActivePolls": "{count, plural, one {# active poll} other {# active polls}}","deleteAccountDataWarning": "All data associated with your account, including your polls, events, votes, and comments, will be permanently deleted.",+ "deleteAccountDialogDescription": "Are you sure you want to delete your account?",+ "deleteAccountDialogImmediateWarning": "Your account and data will be deleted immediately. This cannot be undone.","deleteAccountDialogScheduleDescription": "Your account will be scheduled for deletion.","deleteAccountDialogTitle": "Delete Account",+ "deleteAccountInstruction": "Please confirm your email address to delete your account","deleteAccountRecoveryWindow": "You can cancel the deletion within {count, plural, one {# day} other {# days}}. After that, your account and data will be permanently deleted.","deleteAccountSubscriptionNotice": "Your Pro subscription will not renew and will end with your current billing period. If you cancel the deletion, your subscription will continue as normal.","deleteAccountUpcomingEvents": "{count, plural, one {# upcoming event} other {# upcoming events}}",@@ -258,6 +261,7 @@"emailChangeVerifyDescription": "Enter the 6 digit verification code we sent to {email}.","emailChangeVerifyError": "Verification failed. Please try again.","emailDoesNotMatch": "The email address does not match.",+ "emailMismatch": "Email does not match the account email","emailPlaceholder": "[email protected]","enabled": "Enabled","endOfList": "You've reached the end of the list",diff --git a/apps/web/src/app/[locale]/(space)/settings/profile/actions.ts b/apps/web/src/app/[locale]/(space)/settings/profile/actions.tsindex eeb4f6e9c..a98025020 100644--- a/apps/web/src/app/[locale]/(space)/settings/profile/actions.ts+++ b/apps/web/src/app/[locale]/(space)/settings/profile/actions.ts@@ -12,10 +12,17 @@ import {scheduleAccountDeletion,} from "@/features/user/account-deletion/mutations";import { getScheduledDeletionDate } from "@/features/user/account-deletion/utils";+import { hardDeleteUser } from "@/features/user/mutations";import { getLocale } from "@/i18n/server/get-locale";+import { isSelfHosted } from "@/lib/constants";import { formatDateTime } from "@/lib/datetime/format";import { AppError } from "@/lib/errors/app-error";-import { track } from "@/lib/posthog";+import {+ deletePostHogPerson,+ flushPostHog,+ track,+ trackSystemEvent,+} from "@/lib/posthog";import {authActionClient,createRateLimitMiddleware,@@ -32,6 +39,14 @@ export const scheduleAccountDeletionAction = authActionClient// Each call hits Stripe and sends an email β keep the ceiling low..use(createRateLimitMiddleware(3, "1 h")).action(async ({ ctx }) => {+ if (isSelfHosted) {+ throw new AppError({+ code: "FORBIDDEN",+ message:+ "Self-hosted instances delete accounts immediately instead of scheduling them",+ });+ }+if (ctx.ability.cannot("delete", subject("User", ctx.user))) {throw new AppError({code: "FORBIDDEN",@@ -76,6 +91,36 @@ export const scheduleAccountDeletionAction = authActionClient);});+// Self-hosted instances have no scheduler running the remove-deleted-users+// reaper, so scheduling a deletion would promise a date that never comes.+// Deletion happens immediately instead; there is no recovery window.+export const deleteAccountAction = authActionClient+ .metadata({ actionName: "delete_account" })+ .use(createRateLimitMiddleware(3, "1 h"))+ .action(async ({ ctx }) => {+ if (!isSelfHosted) {+ throw new AppError({+ code: "FORBIDDEN",+ message:+ "Immediate account deletion is only available on self-hosted instances",+ });+ }++ if (ctx.ability.cannot("delete", subject("User", ctx.user))) {+ throw new AppError({+ code: "FORBIDDEN",+ message: "You are not authorized to delete this account",+ });+ }++ await deletePostHogPerson({ distinctId: ctx.user.id });+ await hardDeleteUser({ userId: ctx.user.id, email: ctx.user.email });++ // Personless by design β the person this event is about was just erased.+ trackSystemEvent({ event: "account_deletion_complete" });+ after(() => flushPostHog());+ });+export const cancelAccountDeletionAction = authActionClient.metadata({ actionName: "cancel_account_deletion" }).use(createRateLimitMiddleware(10, "1 h"))diff --git a/apps/web/src/app/[locale]/(space)/settings/profile/components/delete-account-dialog.tsx b/apps/web/src/app/[locale]/(space)/settings/profile/components/delete-account-dialog.tsxindex 516e944f4..f97fa4788 100644--- a/apps/web/src/app/[locale]/(space)/settings/profile/components/delete-account-dialog.tsx+++ b/apps/web/src/app/[locale]/(space)/settings/profile/components/delete-account-dialog.tsx@@ -10,12 +10,30 @@ import {DialogHeader,DialogTitle,} from "@rallly/ui/dialog";+import { Form, FormField, FormItem, FormMessage } from "@rallly/ui/form";+import { Input } from "@rallly/ui/input";+import { useForm } from "react-hook-form";import { ACCOUNT_DELETION_GRACE_DAYS } from "@/features/user/account-deletion/constants";-import { Trans } from "@/i18n/client";+import { useAuthedUser } from "@/features/user/client";+import { Trans, useTranslation } from "@/i18n/client";+import { signOut } from "@/lib/auth-client";+import { isSelfHosted } from "@/lib/constants";import { useSafeAction } from "@/lib/safe-action/client";-import { scheduleAccountDeletionAction } from "../actions";+import { deleteAccountAction, scheduleAccountDeletionAction } from "../actions";-export function DeleteAccountDialog({+export function DeleteAccountDialog(+ props: DialogProps & {+ summary?: React.ReactNode;+ },+) {+ if (isSelfHosted) {+ return <InstantDeleteAccountDialog {...props} />;+ }++ return <ScheduleAccountDeletionDialog {...props} />;+}++function ScheduleAccountDeletionDialog({summary,...rest}: DialogProps & {@@ -76,3 +94,119 @@ export function DeleteAccountDialog({</Dialog>);}++// Self-hosted instances delete immediately β no reaper, no recovery window β+// so the email confirmation step guards against an irreversible mistake.+function InstantDeleteAccountDialog({+ summary,+ ...rest+}: DialogProps & {+ summary?: React.ReactNode;+}) {+ const user = useAuthedUser();+ const { t } = useTranslation();+ const form = useForm<{ email: string }>({+ defaultValues: {+ email: "",+ },+ });++ const deleteAccount = useSafeAction(deleteAccountAction, {+ onSuccess: () => {+ // The server already revoked the session; sign out clears the cookies+ // before leaving the page.+ signOut()+ .catch(() => {})+ .finally(() => {+ window.location.href = "/";+ });+ },+ });++ return (+ <Form {...form}>+ <Dialog {...rest}>+ <DialogContent>+ <form+ onSubmit={form.handleSubmit(async () => {+ await deleteAccount.executeAsync();+ })}+ >+ <DialogHeader>+ <DialogTitle>+ <Trans+ i18nKey="deleteAccountDialogTitle"+ defaults="Delete Account"+ />+ </DialogTitle>+ <DialogDescription>+ <Trans+ i18nKey="deleteAccountDialogDescription"+ defaults="Are you sure you want to delete your account?"+ />+ </DialogDescription>+ </DialogHeader>+ <div className="space-y-3 py-4 text-sm">+ <p>+ <Trans+ i18nKey="deleteAccountDataWarning"+ defaults="All data associated with your account, including your polls, events, votes, and comments, will be permanently deleted."+ />+ </p>+ {summary}+ <p>+ <Trans+ i18nKey="deleteAccountDialogImmediateWarning"+ defaults="Your account and data will be deleted immediately. This cannot be undone."+ />+ </p>+ <p>+ <Trans+ i18nKey="deleteAccountInstruction"+ defaults="Please confirm your email address to delete your account"+ />+ </p>+ <FormField+ control={form.control}+ name="email"+ rules={{+ validate: (value) => {+ if (value !== user.email) {+ return t("emailMismatch", {+ defaultValue: "Email does not match the account email",+ });+ }+ return true;+ },+ }}+ render={({ field }) => (+ <FormItem>+ <Input+ autoComplete="off"+ data-1p-ignore+ placeholder={user.email}+ {...field}+ />+ <FormMessage />+ </FormItem>+ )}+ />+ </div>+ <DialogFooter>+ <DialogClose render={<Button />}>+ <Trans i18nKey="cancel" defaults="Cancel" />+ </DialogClose>+ <Button+ type="submit"+ variant="destructive"+ loading={deleteAccount.isExecuting}+ >+ <Trans i18nKey="deleteAccount" defaults="Delete Account" />+ </Button>+ </DialogFooter>+ </form>+ </DialogContent>+ </Dialog>+ </Form>+ );+}