Files
Momento/memento-note/app/actions/cookie-consent.ts
Antigravity a623454347
Some checks failed
CI / Lint, Unit Tests & Build (push) Failing after 1m32s
CI / Deploy production (on server) (push) Has been skipped
perf: memo GridCard, fuse save fns, fix slash tab active color
2026-06-14 14:06:05 +00:00

72 lines
2.0 KiB
TypeScript

'use server'
import { auth } from '@/auth'
import { updateAISettings } from './ai-settings'
import { getAISettings } from './ai-settings'
import type { ConsentRecord } from '@/lib/consent/cookie-consent'
/**
* Get cookie consent from database for authenticated users.
* Returns null for guests or when no DB preference exists.
*/
export async function getCookieConsentFromDB(): Promise<ConsentRecord | null> {
const session = await auth()
if (!session?.user?.id) {
return null
}
try {
const settings = await getAISettings(session.user.id)
// Only return consent if user has explicitly set it
// If anonymousAnalytics is undefined or false, treat as "no preference set"
if (settings.anonymousAnalytics === true) {
return {
version: 1,
necessary: true,
analytics: true,
marketing: false,
updatedAt: new Date().toISOString(),
}
}
// If explicitly false, return that too
if (settings.anonymousAnalytics === false) {
return {
version: 1,
necessary: true,
analytics: false,
marketing: false,
updatedAt: new Date().toISOString(),
}
}
} catch (error) {
console.error('[getCookieConsentFromDB] Failed to load:', error)
}
return null
}
/**
* Save cookie consent and sync to database for authenticated users.
*
* For logged-in users, this updates UserAISettings.anonymousAnalytics in the database.
* For guests, this no-ops silently (they rely on localStorage only).
*
* Call this from client components after setting local consent.
*/
export async function saveCookieConsent(consent: ConsentRecord) {
const session = await auth()
// Only sync to DB for authenticated users
if (session?.user?.id) {
try {
await updateAISettings({ anonymousAnalytics: consent.analytics })
} catch (error) {
console.error('[saveCookieConsent] Failed to sync to DB:', error)
// Don't throw — local consent is already set, DB sync is best-effort
}
}
return { success: true }
}