- Centralize AI consent 403 responses in server-consent helpers - Return human-readable message + code + i18n errorKey from all AI APIs - Update dashboard and AI clients to detect code/errorKey and translate - Add consent.ai.requiredToast i18n keys (fr/en)
44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
import { auth } from '@/auth'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { NextResponse } from 'next/server'
|
|
|
|
/**
|
|
* Checks if the authenticated user has explicit GDPR AI processing consent.
|
|
* Persistent consent: UserAISettings.aiProcessingConsent
|
|
* Session-only consent: signed JWT claim (not client headers — GDPR-safe)
|
|
*/
|
|
export async function hasUserAiConsent(): Promise<boolean> {
|
|
const session = await auth()
|
|
if (!session?.user?.id) {
|
|
return false
|
|
}
|
|
|
|
if (session.aiSessionConsent === true) {
|
|
return true
|
|
}
|
|
|
|
const settings = await prisma.userAISettings.findUnique({
|
|
where: { userId: session.user.id },
|
|
select: { aiProcessingConsent: true },
|
|
})
|
|
|
|
return settings?.aiProcessingConsent ?? false
|
|
}
|
|
|
|
const DEFAULT_AI_CONSENT_MESSAGE = 'AI processing consent is required to use this feature.'
|
|
const AI_CONSENT_ERROR_KEY = 'consent.ai.requiredToast'
|
|
|
|
export function aiConsentForbiddenResponse(message?: string) {
|
|
return NextResponse.json(
|
|
{ error: message ?? DEFAULT_AI_CONSENT_MESSAGE, code: 'ai_consent_required', errorKey: AI_CONSENT_ERROR_KEY },
|
|
{ status: 403 }
|
|
)
|
|
}
|
|
|
|
export function aiConsentForbiddenJson(message?: string) {
|
|
return NextResponse.json(
|
|
{ success: false, error: message ?? DEFAULT_AI_CONSENT_MESSAGE, code: 'ai_consent_required', errorKey: AI_CONSENT_ERROR_KEY },
|
|
{ status: 403 }
|
|
)
|
|
}
|