58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
|
//
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
// copyright notice and this permission notice appear in all copies.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
import type { ConsentAction } from "./types";
|
|
|
|
export const COOKIE_NAME = "probo_consent";
|
|
const SECONDS_PER_DAY = 86400;
|
|
|
|
export interface ConsentCookie {
|
|
bid: string;
|
|
v: number;
|
|
vid: string;
|
|
action: ConsentAction;
|
|
data: Record<string, boolean>;
|
|
}
|
|
|
|
export function getConsentCookie(): ConsentCookie | null {
|
|
try {
|
|
const prefix = `${COOKIE_NAME}=`;
|
|
const entry = document.cookie
|
|
.split("; ")
|
|
.find((c) => c.startsWith(prefix));
|
|
|
|
if (!entry) {
|
|
return null;
|
|
}
|
|
|
|
return JSON.parse(decodeURIComponent(entry.substring(prefix.length)));
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function setConsentCookie(
|
|
value: ConsentCookie,
|
|
expiryDays: number,
|
|
): void {
|
|
const maxAge = expiryDays * SECONDS_PER_DAY;
|
|
const encoded = encodeURIComponent(JSON.stringify(value));
|
|
|
|
document.cookie = `${COOKIE_NAME}=${encoded}; path=/; max-age=${maxAge}; SameSite=Lax`;
|
|
}
|
|
|
|
export function clearConsentCookie(): void {
|
|
document.cookie = `${COOKIE_NAME}=; path=/; max-age=0; SameSite=Lax`;
|
|
}
|