Files
accounted/app/api/account/delete/route.ts
T
Mattsson 63fd5311ed Bug/tic unlink (#1153)
* fix(tic): allow BankID link/unlink without a company context

/bankid/link and /bankid/unlink are user-level actions, but the extension
dispatcher resolved an active company for them, so a zero-company user
(fresh BankID signup, pre-onboarding) got a 500 'No company context' when
managing the connection from /settings/account. Mark both routes
skipCompanyContext and resolve the caller in-handler via requireAuth(),
which preserves the dispatcher's MFA/AAL2 enforcement.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(tic): return 409 account_exists instead of 500 on BankID signup with taken email

The signup guard pre-checked profiles.email, but the authoritative store
is auth.users: anonymized account tombstones (and any profile drift) hold
the email in auth.users while profiles.email is NULL. The guard missed,
createUser failed with email_exists (422), and the route surfaced a
dead-end 500 'Kunde inte skapa kontot. Forsok igen.' where retrying can
never succeed.

Drop the profiles pre-check and let createUser's own uniqueness check be
the guard: map email_exists to the existing 409 account_exists response
(Swedish message), which the register page already handles with a toast
and a redirect to login. Also removes the TOCTOU window between the old
pre-check and createUser.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(account): actually scrub auth.users metadata on account deletion

The delete route passed user_metadata: {} / app_metadata: {} to
auth.admin.updateUserById assuming replace semantics, but GoTrue MERGES
metadata maps, so the wipe was a silent no-op: the ~100-year tombstone
kept the user's full name in raw_user_meta_data (verified on production
2026-07-24).

Move the scrub into anonymize_user_account (migration 20260724150000):
raw_user_meta_data is cleared entirely, raw_app_meta_data drops the
app-specific keys (bankid_linked, has_password) while GoTrue's
provider/providers stay, and auth.users.email is still retained as the
documented legitimate-interest tombstone. The migration also repairs
existing tombstones (guarded by profiles.anonymized_at). The route keeps
only the ban, which the DB function cannot set.

Migration content already applied to staging; pg-real test extended.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs: log BankID signup guard decision

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(account): address PR review findings on anonymize scrub

- anonymize_user_account now rejects repeat invocations against an
  already-anonymized tombstone (SQLSTATE P0002) instead of re-churning
  the scrubbed row
- note that the tombstone repair UPDATE runs atomically inside the
  migration transaction
- tic signup failure log hashes the email (sha256 prefix, matching the
  pnrHashPrefix pattern) instead of logging the raw address
- pg-real test for the double-invocation guard

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(anonymization): ensure raw_app_meta_data is not null before scrubbing keys

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-24 16:36:35 +02:00

139 lines
5.2 KiB
TypeScript

import { createServiceClient } from '@/lib/supabase/server'
import { NextResponse } from 'next/server'
import { z } from 'zod'
import { ensureInitialized } from '@/lib/init'
import { requireAuth } from '@/lib/auth/require-auth'
import { validateBody } from '@/lib/api/validate'
import { eventBus } from '@/lib/events'
import { createLogger } from '@/lib/logger'
const log = createLogger('api/account/delete')
ensureInitialized()
const DeleteAccountSchema = z.object({
confirm_email: z.string().email(),
})
/**
* POST /api/account/delete
*
* Anonymizes the calling user's account. The auth.users row is retained
* (banned for ~100 years) as a tombstone so FKs into BFL-retained
* bookkeeping data (companies.created_by, audit_log.user_id, etc.) stay
* valid. Memberships are removed, profile PII is stripped, and a global
* signout forces all sessions to end.
*
* Precondition: the user must own zero non-archived companies. The RPC
* enforces this at the DB level and raises SQLSTATE P0001 with a message
* if the precondition fails: we return 409 in that case.
*
* Not wrapped in withRouteContext: deletion must work for users with zero
* companies, so there is no company context to resolve. requireAuth() is
* used directly so MFA (AAL2) is still enforced on hosted: a stolen AAL1
* cookie must not be able to destroy the account. BankID-linked users are
* exempt from the AAL2 gate (BankID is inherently 2FA, see shouldEnforceMfa).
*/
export async function POST(request: Request) {
const auth = await requireAuth()
if (auth.error) return auth.error
const { user, supabase } = auth
const result = await validateBody(request, DeleteAccountSchema)
if (!result.success) return result.response
const { confirm_email } = result.data
if (!user.email || confirm_email.trim().toLowerCase() !== user.email.toLowerCase()) {
return NextResponse.json(
{ error: 'E-postadressen stämmer inte överens med ditt konto.' },
{ status: 400 }
)
}
// Anonymize in the DB. Runs as SECURITY DEFINER and checks auth.uid()
// internally, so we don't need service role here.
const { error: rpcError } = await supabase.rpc('anonymize_user_account', {
target_user_id: user.id,
})
if (rpcError) {
// P0001 = precondition violation from our RPC: user still owns active
// companies. Re-fetch the blockers and return 409 so the UI can show
// the list inline.
if (rpcError.code === 'P0001') {
const service = createServiceClient()
const { data: blockers } = await service
.from('company_members')
.select('company_id, companies!inner(id, name, archived_at)')
.eq('user_id', user.id)
.eq('role', 'owner')
.is('companies.archived_at', null)
const list = (blockers ?? []).map((b) => {
const company = (b.companies as unknown) as { id: string; name: string }
return { id: company.id, name: company.name }
})
return NextResponse.json(
{
error: 'Du måste radera eller överlåta dina företag innan du kan radera kontot.',
blockers: list,
},
{ status: 409 }
)
}
log.error('anonymize_user_account failed', { userId: user.id, error: rpcError.message })
return NextResponse.json(
{ error: 'Kunde inte radera kontot. Försök igen.' },
{ status: 500 }
)
}
// Ban the tombstone row ~100 years so login is impossible. The DB function
// can't set the ban (GoTrue-managed), so we do it here.
//
// Note: auth.users.email is intentionally NOT scrubbed. The original
// address is retained as a legitimate-interest tombstone so that:
// (1) re-signup with the same email is blocked by Supabase's unique
// constraint: deletion must feel permanent, not trivially
// reversible by re-registering
// (2) support can verify identity when a former user asks to recover
// BFL-retained räkenskapsinformation
// This must be documented in the privacy policy under legitimate
// interest (GDPR Art. 6(1)(f)). The email is never read by the app
// after this point: login is impossible (row is banned) and the
// profile is anonymized, so no UI ever surfaces it.
//
// user_metadata / app_metadata PII is scrubbed by the RPC itself, NOT
// here: GoTrue's admin update MERGES metadata maps, so the previous
// updateUserById(..., { user_metadata: {}, app_metadata: {} }) call was
// a silent no-op that left the full name on the tombstone (found on
// prod 2026-07-24, repaired by migration 20260724150000).
const service = createServiceClient()
try {
await service.auth.admin.updateUserById(user.id, {
ban_duration: '876000h',
})
} catch (err) {
log.error('Failed to ban anonymized user', { userId: user.id, err })
}
try {
await service.auth.admin.signOut(user.id, 'global')
} catch (err) {
log.error('Failed to global sign out anonymized user', { userId: user.id, err })
}
const deletedAt = new Date().toISOString()
await eventBus.emit({
type: 'account.deleted',
payload: { userId: user.id, deletedAt },
})
// Best-effort: clear the caller's session cookie too.
await supabase.auth.signOut().catch(() => {})
return NextResponse.json({ success: true })
}