"use client"; /** * Admin "view as user". The admin's own token (and cached profile) are put aside while * the impersonation token is the active one, and restored when it ends. Everything is * read-only on the server (any write is refused), so this is only about which token the * browser sends. */ import { API_BASE } from "@/lib/api"; import { hardNavigate } from "@/lib/navigation"; const TOKEN_KEY = "hope_events_token"; const USER_CACHE_KEY = "hope_events_user_cache"; const ADMIN_TOKEN_KEY = "hope_events_admin_token"; const ADMIN_CACHE_KEY = "hope_events_admin_user_cache"; const IMP_KEY = "hope_events_impersonation"; export type Impersonation = { sessionId: string; expiresAt: string; name: string; email: string }; export function currentImpersonation(): Impersonation | null { try { const raw = localStorage.getItem(IMP_KEY); return raw ? JSON.parse(raw) : null; } catch { return null; } } export const isImpersonating = () => !!currentImpersonation(); /** Switches this browser to the impersonation token and opens the user's dashboard. */ export type ImpersonationStart = { token: string; session: { id: string; expiresAt: string; target: { name: string; email: string } } }; export function beginImpersonation(res: ImpersonationStart) { try { localStorage.setItem(ADMIN_TOKEN_KEY, localStorage.getItem(TOKEN_KEY) || ""); localStorage.setItem(ADMIN_CACHE_KEY, localStorage.getItem(USER_CACHE_KEY) || ""); localStorage.setItem(TOKEN_KEY, res.token); localStorage.removeItem(USER_CACHE_KEY); localStorage.setItem(IMP_KEY, JSON.stringify({ sessionId: res.session.id, expiresAt: res.session.expiresAt, name: res.session.target.name, email: res.session.target.email })); } catch { /* storage unavailable */ } hardNavigate("/dashboard/user"); } /** Puts the admin's own session back (no server call). */ export function restoreAdmin(redirect = true) { try { const adminToken = localStorage.getItem(ADMIN_TOKEN_KEY); const adminCache = localStorage.getItem(ADMIN_CACHE_KEY); if (adminToken) localStorage.setItem(TOKEN_KEY, adminToken); else localStorage.removeItem(TOKEN_KEY); if (adminCache) localStorage.setItem(USER_CACHE_KEY, adminCache); else localStorage.removeItem(USER_CACHE_KEY); localStorage.removeItem(ADMIN_TOKEN_KEY); localStorage.removeItem(ADMIN_CACHE_KEY); localStorage.removeItem(IMP_KEY); } catch { /* storage unavailable */ } if (redirect) hardNavigate("/dashboard/admin/users"); } /** Ends the session on the server (audited), then restores the admin. */ export async function endImpersonation() { try { const token = localStorage.getItem(TOKEN_KEY); if (token) await fetch(`${API_BASE}/api/v2/me/impersonation/end`, { method: "POST", headers: { Authorization: `Bearer ${token}` } }); } catch { /* the session expires by itself anyway */ } restoreAdmin(); } /** Records a page view in the session's audit trail. */ export function logImpersonatedPage(path: string) { try { const token = localStorage.getItem(TOKEN_KEY); if (!token) return; fetch(`${API_BASE}/api/v2/me/impersonation/log`, { method: "POST", headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" }, body: JSON.stringify({ path }), keepalive: true }).catch(() => {}); } catch { /* ignore */ } }