Files
HealthLog/src/hooks/use-grant-ai-consent.ts
T
Marc-André Bombeck e2b7e7350e fix(consent): mint AI consent receipts only on an explicit agreement
The AI settings page posted to /api/consent/ai/web on every mount and the
server minted an ai_full receipt for it, so an account that had agreed to
nothing sent its health data to the operator's provider from background
jobs. Native builds synced a receipt the same way on every launch.

- Both grant routes now require `confirm: true`; anything else writes
  nothing and answers 400 consent.ai.confirmRequired.
- Remove the mount-time heal. A consent card names the operator's service
  and the data that would go there, with one Agree button, in Settings > AI
  and on the Insights overview while AI is closed by the missing consent
  alone.
- Publish the standing consent as `ai.consent` on /api/auth/me (status,
  since, recipients); the settings card renders active-since and the
  withdraw control from it.
- Existing receipts stay valid.
- Guard that no client grants outside a click handler, route tests for the
  confirmation, and an integration test that a fresh operator-key account
  gets no background AI text until it agrees.
2026-10-16 17:11:04 +02:00

37 lines
1.2 KiB
TypeScript

"use client";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { apiPost } from "@/lib/api/api-fetch";
import {
aiInputDependentKeys,
invalidateKeys,
queryKeys,
} from "@/lib/query-keys";
/**
* Record the person's explicit AI consent (`ai_full`).
*
* Call `mutate()` from the click handler of an "Agree" control that has
* named the service and the data, and from nowhere else: the server records
* this request as the person's consent act, and `confirm: true` is how the
* request says so. A request on mount, on launch or after a sync is exactly
* what v1.43.2 removed; `consent-grant-explicit-guard.test.ts` keeps it out.
*
* On success `/api/auth/me` is refetched, because the capabilities and the
* `ai.consent` state every AI surface renders from changed.
*/
export function useGrantAiConsent() {
const queryClient = useQueryClient();
return useMutation({
mutationKey: queryKeys.aiConsentReceipt("ai_full"),
mutationFn: () => apiPost("/api/consent/ai/web", { confirm: true }),
onSuccess: async () => {
await invalidateKeys(queryClient, aiInputDependentKeys);
await queryClient.invalidateQueries({
queryKey: queryKeys.aiConsentReceipt("ai_full"),
});
},
});
}