mirror of
https://github.com/fosrl/pangolin.git
synced 2026-07-17 11:06:30 +02:00
33 lines
822 B
TypeScript
33 lines
822 B
TypeScript
/**
|
|
* Set a cookie on the client side in javascript code, not on the server
|
|
* @param name
|
|
* @param value
|
|
* @param days
|
|
* @param options
|
|
*/
|
|
export function setClientCookie(
|
|
name: string,
|
|
value: string,
|
|
options: {
|
|
days?: number;
|
|
path?: string;
|
|
secure?: boolean;
|
|
sameSite?: "Strict" | "Lax" | "None";
|
|
} = {}
|
|
): void {
|
|
let cookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
|
|
|
|
if (options.days) {
|
|
const date = new Date();
|
|
date.setTime(date.getTime() + options.days * 864e5);
|
|
cookie += `; expires=${date.toUTCString()}`;
|
|
}
|
|
|
|
cookie += `; path=${options.path ?? "/"}`;
|
|
|
|
if (options.secure) cookie += "; Secure";
|
|
if (options.sameSite) cookie += `; SameSite=${options.sameSite}`;
|
|
|
|
document.cookie = cookie;
|
|
}
|