mirror of
https://github.com/documenso/documenso.git
synced 2025-11-10 04:22:32 +10:00
feat: require confirmation for user account deletion (#1009)
### This PR adds the necessary user friction in the Delete Dialog, ensuring that users are intentionally deleting their accounts. - User must disable 2FA to delete the account.  - Explicit user confirmation  fixes #998
This commit is contained in:
@ -1,5 +1,7 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
|
import { useState } from 'react';
|
||||||
|
|
||||||
import { signOut } from 'next-auth/react';
|
import { signOut } from 'next-auth/react';
|
||||||
|
|
||||||
import type { User } from '@documenso/prisma/client';
|
import type { User } from '@documenso/prisma/client';
|
||||||
@ -16,6 +18,8 @@ import {
|
|||||||
DialogTitle,
|
DialogTitle,
|
||||||
DialogTrigger,
|
DialogTrigger,
|
||||||
} from '@documenso/ui/primitives/dialog';
|
} from '@documenso/ui/primitives/dialog';
|
||||||
|
import { Input } from '@documenso/ui/primitives/input';
|
||||||
|
import { Label } from '@documenso/ui/primitives/label';
|
||||||
import { useToast } from '@documenso/ui/primitives/use-toast';
|
import { useToast } from '@documenso/ui/primitives/use-toast';
|
||||||
|
|
||||||
export type DeleteAccountDialogProps = {
|
export type DeleteAccountDialogProps = {
|
||||||
@ -28,6 +32,8 @@ export const DeleteAccountDialog = ({ className, user }: DeleteAccountDialogProp
|
|||||||
|
|
||||||
const hasTwoFactorAuthentication = user.twoFactorEnabled;
|
const hasTwoFactorAuthentication = user.twoFactorEnabled;
|
||||||
|
|
||||||
|
const [enteredEmail, setEnteredEmail] = useState<string>('');
|
||||||
|
|
||||||
const { mutateAsync: deleteAccount, isLoading: isDeletingAccount } =
|
const { mutateAsync: deleteAccount, isLoading: isDeletingAccount } =
|
||||||
trpc.profile.deleteAccount.useMutation();
|
trpc.profile.deleteAccount.useMutation();
|
||||||
|
|
||||||
@ -76,10 +82,11 @@ export const DeleteAccountDialog = ({ className, user }: DeleteAccountDialogProp
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex-shrink-0">
|
<div className="flex-shrink-0">
|
||||||
<Dialog>
|
<Dialog onOpenChange={() => setEnteredEmail('')}>
|
||||||
<DialogTrigger asChild>
|
<DialogTrigger asChild>
|
||||||
<Button variant="destructive">Delete Account</Button>
|
<Button variant="destructive">Delete Account</Button>
|
||||||
</DialogTrigger>
|
</DialogTrigger>
|
||||||
|
|
||||||
<DialogContent>
|
<DialogContent>
|
||||||
<DialogHeader className="space-y-4">
|
<DialogHeader className="space-y-4">
|
||||||
<DialogTitle>Delete Account</DialogTitle>
|
<DialogTitle>Delete Account</DialogTitle>
|
||||||
@ -105,12 +112,29 @@ export const DeleteAccountDialog = ({ className, user }: DeleteAccountDialogProp
|
|||||||
</DialogDescription>
|
</DialogDescription>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
|
|
||||||
|
{!hasTwoFactorAuthentication && (
|
||||||
|
<div className="mt-4">
|
||||||
|
<Label>
|
||||||
|
Please type{' '}
|
||||||
|
<span className="text-muted-foreground font-semibold">{user.email}</span> to
|
||||||
|
confirm.
|
||||||
|
</Label>
|
||||||
|
|
||||||
|
<Input
|
||||||
|
type="text"
|
||||||
|
className="mt-2"
|
||||||
|
aria-label="Confirm Email"
|
||||||
|
value={enteredEmail}
|
||||||
|
onChange={(e) => setEnteredEmail(e.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
<DialogFooter>
|
<DialogFooter>
|
||||||
<Button
|
<Button
|
||||||
onClick={onDeleteAccount}
|
onClick={onDeleteAccount}
|
||||||
loading={isDeletingAccount}
|
loading={isDeletingAccount}
|
||||||
variant="destructive"
|
variant="destructive"
|
||||||
disabled={hasTwoFactorAuthentication}
|
disabled={hasTwoFactorAuthentication || enteredEmail !== user.email}
|
||||||
>
|
>
|
||||||
{isDeletingAccount ? 'Deleting account...' : 'Confirm Deletion'}
|
{isDeletingAccount ? 'Deleting account...' : 'Confirm Deletion'}
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
@ -16,6 +16,8 @@ test('delete user', async ({ page }) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await page.getByRole('button', { name: 'Delete Account' }).click();
|
await page.getByRole('button', { name: 'Delete Account' }).click();
|
||||||
|
await page.getByLabel('Confirm Email').fill(user.email);
|
||||||
|
await expect(page.getByRole('button', { name: 'Confirm Deletion' })).not.toBeDisabled();
|
||||||
await page.getByRole('button', { name: 'Confirm Deletion' }).click();
|
await page.getByRole('button', { name: 'Confirm Deletion' }).click();
|
||||||
|
|
||||||
await page.waitForURL(`${WEBAPP_BASE_URL}/signin`);
|
await page.waitForURL(`${WEBAPP_BASE_URL}/signin`);
|
||||||
|
|||||||
Reference in New Issue
Block a user