Files
MichilisandClaude Opus 5 0d7651b17c phase-2: identity, from the landing hook to the profile screen
Flows A1 to A6 and E3 end to end. A visitor types a RUC on the landing page,
sees their real filing dates, registers, verifies a six digit code, grants
consent, completes a three step setup and lands on the first run screen, with
the profile, consent and audit rows to show for it.

API: public RUC lookup behind a token bucket (10/min/IP), the full /me surface
(profile, dependents, consents, notification prefs, data export, account
deletion), an append-only audit module that exports an insert and nothing else,
and a PII module that is the only thing allowed near those tables.

Deletion and consent revocation both freeze the account and drop every session,
reusing better-auth's ban flag rather than adding a second notion of disabled.
Nothing is destroyed yet: the purge is a job for phase 4. deadlineDigit is
always derived server side, never accepted from the client.

Web: landing with the RUC hook, registration, OTP verification, consent, the
setup wizard, the profile screen with "Tus datos", and legal pages that ship as
marked placeholders per COPY.md section 13. Money, Skeleton, Switch and
EmptyState components added.

The seed is now complete for identity: Maria at 4123456-1, filing digit 6 and
day 19, with a dependant, consents and prefs; Carlos as an IVA-only company.

Two real defects found by building the screens and fixed with tests:
the OTP boxes dropped a digit because the handler fired effects inside a
setState updater that React 19 invokes twice, and the switch knob rendered
outside its track because translate-x-5.5 does not resolve.

232 vitest tests, 26 Playwright tests across mobile and desktop, coverage still
100% on the rules, typecheck and lint clean.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-03 23:33:10 +00:00

133 lines
4.4 KiB
TypeScript

'use client';
import { useMutation, useQuery } from '@tanstack/react-query';
import { useRef, useState } from 'react';
import { Button } from '@/components/ui/button';
import { Card } from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { useT } from '@/i18n/t';
import { api } from '@/lib/api';
import { authClient } from '@/lib/auth-client';
/**
* "Tus datos", the trust section of FLOWS.md E3: what we hold, the consent behind it,
* and the two ways to take it back. The export is a plain link so the browser downloads
* it; deletion is a native dialog that will not proceed until the email is typed out.
*/
export function DangerZone() {
const t = useT();
const dialog = useRef<HTMLDialogElement>(null);
const [confirmText, setConfirmText] = useState('');
const session = useQuery({
queryKey: ['session'],
queryFn: async () => (await authClient.getSession()).data,
});
const email = session.data?.user.email ?? '';
const revoke = useMutation({
mutationFn: () => api.postConsent({ kind: 'data_processing', granted: false }),
onSuccess: () => window.location.assign('/'),
});
const remove = useMutation({
mutationFn: () => api.deleteAccount({ confirmText }),
onSuccess: () => window.location.assign('/'),
});
return (
<Card className="space-y-5">
<div>
<h2 className="font-semibold tracking-tight">{t('profile.myData.title')}</h2>
<p className="text-sm text-[var(--text-muted)] text-pretty">{t('profile.myData.body')}</p>
</div>
<p className="text-sm text-[var(--text-muted)] text-pretty">{t('profile.myData.stored')}</p>
{/* A plain link, not a fetch: the browser saves the file and the API sets the name. */}
<a
href="/api/me/data-export"
download
className="inline-flex text-sm font-medium text-accent-700 hover:underline"
>
{t('profile.myData.export')}
</a>
<div className="space-y-2 border-t pt-4">
<p className="text-sm text-[var(--text-muted)] text-pretty">
{t('profile.consent.revokeWarn')}
</p>
<Button
type="button"
variant="secondary"
disabled={revoke.isPending}
onClick={() => revoke.mutate()}
>
{t('profile.consent.revoke')}
</Button>
</div>
<div className="space-y-2 border-t pt-4">
<p className="text-sm text-overdue text-pretty">{t('profile.myData.deleteWarn')}</p>
<Button
type="button"
variant="secondary"
className="border-overdue text-overdue"
onClick={() => dialog.current?.showModal()}
>
{t('profile.myData.delete')}
</Button>
</div>
<dialog
ref={dialog}
aria-label={t('profile.myData.delete')}
className="m-auto w-[min(28rem,calc(100vw-2rem))] rounded-2xl border bg-[var(--surface-raised)] p-6 text-[var(--text)] backdrop:bg-black/40"
>
<div className="space-y-4">
<h3 className="text-lg font-semibold tracking-tight">{t('profile.myData.delete')}</h3>
<p className="text-sm text-[var(--text-muted)] text-pretty">
{t('profile.myData.deleteWarn')}
</p>
<div className="space-y-1.5">
<Label htmlFor="confirmText">{t('profile.myData.deletePrompt', { email })}</Label>
<Input
id="confirmText"
value={confirmText}
autoComplete="off"
aria-invalid={remove.isError}
onChange={(event) => setConfirmText(event.target.value)}
/>
</div>
{remove.isError ? (
<p role="alert" className="text-sm text-overdue">
{t('common.error.generic')}
</p>
) : null}
<div className="flex justify-end gap-2">
<Button type="button" variant="ghost" onClick={() => dialog.current?.close()}>
{t('common.cancel')}
</Button>
<Button
type="button"
className="bg-overdue hover:bg-overdue"
disabled={
remove.isPending ||
confirmText.trim().toLowerCase() !== email.toLowerCase() ||
email === ''
}
onClick={() => remove.mutate()}
>
{t('profile.myData.deleteCta')}
</Button>
</div>
</div>
</dialog>
</Card>
);
}