25 lines
685 B
TypeScript
25 lines
685 B
TypeScript
![]() |
'use client';
|
||
|
|
||
|
export function getClientCookie(key: string): string | null {
|
||
|
if (typeof document === 'undefined') return null;
|
||
|
|
||
|
const value = document.cookie
|
||
|
.split('; ')
|
||
|
.find(row => row.startsWith(`${key}=`))
|
||
|
?.split('=')[1];
|
||
|
|
||
|
return value ? decodeURIComponent(value) : null;
|
||
|
}
|
||
|
|
||
|
export function setClientCookie(
|
||
|
key: string,
|
||
|
value: string,
|
||
|
options: { expires?: Date; path?: string } = {}
|
||
|
): void {
|
||
|
if (typeof document === 'undefined') return;
|
||
|
|
||
|
let cookie = `${key}=${encodeURIComponent(value)}`;
|
||
|
if (options.expires) cookie += `; expires=${options.expires.toUTCString()}`;
|
||
|
cookie += `; path=${options.path || '/'}`;
|
||
|
document.cookie = cookie;
|
||
|
}
|