19 lines
617 B
TypeScript
19 lines
617 B
TypeScript
|
|
export function setCookie(name: string, value: string, days = 365): void {
|
||
|
|
const expires = new Date(Date.now() + days * 24 * 60 * 60 * 1000).toUTCString();
|
||
|
|
document.cookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)}; expires=${expires}; path=/; SameSite=Lax`;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getCookie(name: string): string | null {
|
||
|
|
const key = `${encodeURIComponent(name)}=`;
|
||
|
|
const parts = document.cookie.split(';');
|
||
|
|
for (const part of parts) {
|
||
|
|
const trimmed = part.trim();
|
||
|
|
if (trimmed.startsWith(key)) {
|
||
|
|
return decodeURIComponent(trimmed.slice(key.length));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
|