/** * Session ID utility for custom POI auth. * Stored in localStorage under "session_id" key. * Returns empty string during SSR (window not available). * * ВАЖНО: `crypto.randomUUID()` доступен только в secure context (HTTPS / localhost). * На HTTP-страницах (например, http://94.228.121.73:8091) он undefined → fallback на ручную генерацию. */ function generateUUID(): string { // Native — если есть и работает (HTTPS / localhost / file://) if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") { try { return crypto.randomUUID(); } catch { // редкие браузеры могут бросить SecurityError — падаем в ручной fallback } } // Fallback: v4 UUID через Math.random (НЕ криптостойко, но для session_id хватает) // Шаблон: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx где y ∈ {8,9,a,b} return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => { const r = (Math.random() * 16) | 0; const v = c === "x" ? r : (r & 0x3) | 0x8; return v.toString(16); }); } export function getOrCreateSessionId(): string { if (typeof window === "undefined") return ""; let id = localStorage.getItem("session_id"); if (!id) { id = generateUUID(); localStorage.setItem("session_id", id); } return id; }