This repository has been archived on 2026-11-23. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
hope-events/frontend/src/lib/impersonation.ts
T
joshuaandClaude Opus 5.5 b2439a781e Type more frontend pages and fix their hook dependencies
Cashup, users, profile, consent, allocations, event options, forms,
event tickets, ticket scanning and manual registration now use real
types. Data loaders are wrapped in useCallback and listed as effect
dependencies. Also adds shared Attendee, Section and form response
types.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 11:10:01 +02:00

75 lines
3.2 KiB
TypeScript

"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";
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 */ }
window.location.href = "/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) window.location.href = "/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/admin/impersonate/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/admin/impersonate/log`, { method: "POST", headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" }, body: JSON.stringify({ path }), keepalive: true }).catch(() => {});
} catch { /* ignore */ }
}