/** * Cookie utility functions for browser-based cookie management */ /** * Get a cookie value by name * @param name - The name of the cookie * @returns The cookie value or null if not found */ export const getCookie = (name: string): string | null => { if (typeof window === 'undefined') return null; const value = `; ${document.cookie}`; const parts = value.split(`; ${name}=`); if (parts.length === 2) { const cookieValue = parts.pop()?.split(';').shift(); return cookieValue ? decodeURIComponent(cookieValue) : null; } return null; }; /** * Set a cookie with an expiration date * @param name - The name of the cookie * @param value - The value to store * @param days - Number of days until expiration (default: 30) */ export const setCookie = (name: string, value: string, days: number = 30) => { if (typeof window === 'undefined') return; const expires = new Date(); expires.setTime(expires.getTime() + (days * 24 * 60 * 60 * 1000)); document.cookie = `${name}=${encodeURIComponent(value)};expires=${expires.toUTCString()};path=/;SameSite=Lax`; }; /** * Delete a cookie by setting its expiration to the past * @param name - The name of the cookie to delete */ export const deleteCookie = (name: string) => { if (typeof window === 'undefined') return; document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 UTC;path=/;SameSite=Lax`; }; /** * Check if a cookie exists * @param name - The name of the cookie * @returns True if the cookie exists, false otherwise */ export const cookieExists = (name: string): boolean => { return getCookie(name) !== null; };