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mBAAA,CAAoB,oBAAoB,KAAK,CAAA;AACpD,IAAA,MAAA,CAAO,mBAAA,CAAoB,SAAS,KAAK,CAAA;AACzC,IAAA,MAAA,CAAO,mBAAA,CAAoB,YAAY,KAAK,CAAA;AAC5C,IAAA,IAAI;AACF,MAAA,OAAA,EAAS,KAAA,EAAM;AAAA,IACjB,CAAA,CAAA,MAAQ;AAAA,IAER;AAAA,EACF;AAEA,EAAA,OAAO,OAAA;AACT","file":"logout-signal.cjs","sourcesContent":["// Universal sign-out propagation — the ONE cross-app logout contract.\n//\n// Every Politogy app holds its OWN host-only session cookie with a long sliding\n// window, so signing out of one app used to leave every other open tab — and\n// every other subdomain — signed in until its cookie happened to expire. A user\n// who signs out in Contacts and switches to the Forms tab they already had open\n// is still fully authenticated there. That is the bug this module closes.\n//\n// The vehicle mirrors the active-org contract: a `.politogyvrm.com` cookie that\n// every subdomain can read. It carries a single monotonic value — the epoch\n// milliseconds at which the user signed out.\n//\n//   politogy_logout_at = <epoch ms of the most recent sign-out>\n//\n// Two layers cooperate, and BOTH are required:\n//\n//   1. SERVER (authoritative). Each app's middleware compares this cookie\n//      against the session's `auth_time`. A session minted BEFORE the last\n//      sign-out is dead, whatever its own expiry says — so a reload, a\n//      bookmark, or a restored tab can never resurrect it. See\n//      `isSessionRevoked`.\n//\n//   2. CLIENT (immediacy). Open tabs are not making requests, so the server\n//      layer alone only takes effect on their next navigation. `watchLogout`\n//      notices the cookie change within ~1s and navigates the tab out.\n//\n// The cookie is deliberately NOT HttpOnly: it holds no secret — just a\n// timestamp — and every tab on every subdomain must be able to read it from\n// JavaScript. It grants nothing; it only ever revokes.\n//\n// Server-safe (no 'use client'): the constants and `isSessionRevoked` are\n// importable from middleware and server components; the browser helpers no-op\n// on the server.\n\nexport const LOGOUT_COOKIE = 'politogy_logout_at'\n\n/** Same-origin instant channel; cross-subdomain falls back to the cookie poll. */\nconst LOGOUT_CHANNEL = 'politogy-logout'\n\n/** How often a tab re-reads the cookie. Cheap: a string compare, no I/O. */\nconst POLL_MS = 1000\n\nfunction parseTimestamp(value: string | null | undefined): number | null {\n  if (!value) return null\n  const n = Number(value)\n  return Number.isFinite(n) && n > 0 ? n : null\n}\n\n/**\n * Server-side authority. Given the logout-cookie value and the moment this\n * session was authenticated, is the session revoked?\n *\n * `authTime` is the OIDC `auth_time` (seconds) or an equivalent millisecond\n * timestamp captured when the session was minted. Sessions with no recorded\n * auth time are treated as revoked whenever a logout is on record: we cannot\n * prove they postdate it, and failing closed is the only safe direction for a\n * sign-out.\n */\nexport function isSessionRevoked(\n  logoutCookieValue: string | null | undefined,\n  authTime: number | null | undefined,\n): boolean {\n  const loggedOutAt = parseTimestamp(logoutCookieValue)\n  if (loggedOutAt === null) return false\n  if (authTime == null || !Number.isFinite(authTime) || authTime <= 0) return true\n  // Accept either seconds (OIDC auth_time) or milliseconds.\n  const authMs = authTime < 1e12 ? authTime * 1000 : authTime\n  return authMs < loggedOutAt\n}\n\n/** Read the shared logout timestamp (browser only; null on server or unset). */\nexport function readLogoutCookie(): number | null {\n  if (typeof document === 'undefined') return null\n  try {\n    const m = document.cookie.match(new RegExp(`(?:^|;\\\\s*)${LOGOUT_COOKIE}=([^;]+)`))\n    return parseTimestamp(m ? decodeURIComponent(m[1]) : null)\n  } catch {\n    return null\n  }\n}\n\n/**\n * Record a sign-out for every tab on every Politogy subdomain. Call this\n * BEFORE navigating to the app's sign-out route, so tabs that are already open\n * see it immediately rather than after the round trip.\n *\n * Scoping mirrors the active-org and theme cookies: parent-domain on\n * *.politogyvrm.com so all app subdomains read it, host-only anywhere else\n * (localhost, *.pages.dev previews).\n */\nexport function broadcastLogout(at: number = Date.now()): void {\n  if (typeof document === 'undefined' || typeof window === 'undefined') return\n  const host = window.location.hostname\n  const domainAttr = /(^|\\.)politogyvrm\\.com$/i.test(host) ? '; Domain=.politogyvrm.com' : ''\n  const secure = window.location.protocol === 'https:' ? '; Secure' : ''\n  // 30 days: comfortably longer than the longest app session window, so a tab\n  // resumed from sleep still sees the revocation. Shorter than that and a stale\n  // session could outlive the signal that killed it.\n  document.cookie = `${LOGOUT_COOKIE}=${at}; Path=/; Max-Age=2592000; SameSite=Lax${domainAttr}${secure}`\n  try {\n    new BroadcastChannel(LOGOUT_CHANNEL).postMessage(at)\n  } catch {\n    // No BroadcastChannel (older Safari) — the cookie poll covers it.\n  }\n}\n\n/**\n * Clear the signal. Call after a successful LOGIN: the new session postdates\n * the old sign-out, and leaving the cookie behind would make every future\n * `isSessionRevoked` check depend on clock skew between the two.\n */\nexport function clearLogoutSignal(): void {\n  if (typeof document === 'undefined' || typeof window === 'undefined') return\n  const host = window.location.hostname\n  const domainAttr = /(^|\\.)politogyvrm\\.com$/i.test(host) ? '; Domain=.politogyvrm.com' : ''\n  document.cookie = `${LOGOUT_COOKIE}=; Path=/; Max-Age=0; SameSite=Lax${domainAttr}`\n}\n\nexport type WatchLogoutOptions = {\n  /**\n   * Sessions minted at or before this moment are revoked by a sign-out. Pass\n   * the session's auth time; when omitted, ANY logout signal observed after the\n   * watcher starts fires the callback (correct for an already-open tab, which\n   * by definition authenticated earlier).\n   */\n  authTime?: number | null\n  /** Poll interval override, for tests. */\n  intervalMs?: number\n}\n\n/**\n * Watch for a sign-out that happened in ANY other tab or window, on this or any\n * sibling Politogy subdomain, and invoke `onLogout` once.\n *\n * Returns an unsubscribe function.\n *\n * Three signals, cheapest first:\n *   - BroadcastChannel — instant, same-origin only.\n *   - visibility/focus/pageshow — the moment a backgrounded tab is looked at.\n *   - a ~1s cookie poll — the cross-subdomain path, and the backstop for a\n *     visible tab the user is staring at but not interacting with.\n */\nexport function watchLogout(\n  onLogout: () => void,\n  options: WatchLogoutOptions = {},\n): () => void {\n  if (typeof document === 'undefined' || typeof window === 'undefined') {\n    return () => {}\n  }\n\n  const { authTime = null, intervalMs = POLL_MS } = options\n  // Anything already on record when this tab started is not news: the server\n  // layer decides whether THIS session is revoked, and it has already let the\n  // page render. Only a signal newer than our baseline means \"someone just\n  // signed out\".\n  const baseline = readLogoutCookie() ?? 0\n  let done = false\n\n  const fire = (at: number) => {\n    if (done) return\n    if (at <= baseline) return\n    if (authTime != null && !isSessionRevoked(String(at), authTime)) return\n    done = true\n    cleanup()\n    onLogout()\n  }\n\n  const check = () => {\n    const at = readLogoutCookie()\n    if (at !== null) fire(at)\n  }\n\n  let channel: BroadcastChannel | null = null\n  try {\n    channel = new BroadcastChannel(LOGOUT_CHANNEL)\n    channel.onmessage = (event) => {\n      const at = typeof event.data === 'number' ? event.data : readLogoutCookie()\n      if (at !== null) fire(at)\n    }\n  } catch {\n    channel = null\n  }\n\n  const timer = window.setInterval(check, intervalMs)\n  window.addEventListener('visibilitychange', check)\n  window.addEventListener('focus', check)\n  window.addEventListener('pageshow', check)\n\n  function cleanup() {\n    window.clearInterval(timer)\n    window.removeEventListener('visibilitychange', check)\n    window.removeEventListener('focus', check)\n    window.removeEventListener('pageshow', check)\n    try {\n      channel?.close()\n    } catch {\n      /* already closed */\n    }\n  }\n\n  return cleanup\n}\n"]}