42 lines
1.3 KiB
JavaScript
42 lines
1.3 KiB
JavaScript
import { CONSENT_CATEGORIES, DEFAULT_CONSENT } from './categories.js';
|
|
/**
|
|
* Consent persistence in a cookie. Stored with a version so we can invalidate
|
|
* old consents when the policy changes, and a timestamp (GDPR: consent must be
|
|
* dated). Readable both client-side (banner) and server-side (script gating).
|
|
*/ export const CONSENT_COOKIE = 'cookie-consent';
|
|
export const CONSENT_VERSION = 1;
|
|
export const CONSENT_MAX_AGE = 60 * 60 * 24 * 180 // 180 days
|
|
;
|
|
export function serializeConsent(state) {
|
|
const payload = {
|
|
state,
|
|
timestamp: new Date().toISOString(),
|
|
version: CONSENT_VERSION
|
|
};
|
|
return encodeURIComponent(JSON.stringify(payload));
|
|
}
|
|
export function parseConsent(raw) {
|
|
if (!raw) {
|
|
return null;
|
|
}
|
|
try {
|
|
const parsed = JSON.parse(decodeURIComponent(raw));
|
|
if (parsed.version !== CONSENT_VERSION) {
|
|
return null;
|
|
}
|
|
const state = {
|
|
...DEFAULT_CONSENT
|
|
};
|
|
for (const cat of CONSENT_CATEGORIES){
|
|
if (typeof parsed.state?.[cat] === 'boolean') {
|
|
state[cat] = parsed.state[cat];
|
|
}
|
|
}
|
|
state.necessary = true;
|
|
return state;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
//# sourceMappingURL=storage.js.map
|