Archived
Add read-only admin impersonation
Admins (with 2FA) can view the site as a non-admin user for up to 30 minutes, with a required reason and a "Viewing as" banner. It is strictly read-only: a guard mounted before every route refuses any write made with an impersonation token, protect/optionalAuth check the session is live, and GET handlers skip the price-refresh writes. Start, end, reason and every page/API call (including blocked attempts) are audited; optional email to the user. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CP62L9JxHomA36aYP1ekCe
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
"use client";
|
||||
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { usePathname } from "next/navigation";
|
||||
import { Eye } from "lucide-react";
|
||||
import { currentImpersonation, endImpersonation, logImpersonatedPage, type Impersonation } from "@/lib/impersonation";
|
||||
|
||||
/** Bright bar on every page while an admin is viewing the site as someone else. */
|
||||
export function ImpersonationBanner() {
|
||||
const pathname = usePathname();
|
||||
const [imp, setImp] = useState<Impersonation | null>(null);
|
||||
const [left, setLeft] = useState("");
|
||||
const [ending, setEnding] = useState(false);
|
||||
|
||||
useEffect(() => { setImp(currentImpersonation()); }, []);
|
||||
|
||||
// Every page viewed goes into the session's audit trail.
|
||||
useEffect(() => {
|
||||
if (imp && pathname) logImpersonatedPage(pathname + (typeof window !== "undefined" ? window.location.search : ""));
|
||||
}, [imp, pathname]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!imp) return;
|
||||
const tick = () => {
|
||||
const ms = new Date(imp.expiresAt).getTime() - Date.now();
|
||||
if (ms <= 0) { endImpersonation(); return; }
|
||||
const m = Math.floor(ms / 60000), s = Math.floor((ms % 60000) / 1000);
|
||||
setLeft(`${m}:${String(s).padStart(2, "0")}`);
|
||||
};
|
||||
tick();
|
||||
const t = setInterval(tick, 1000);
|
||||
return () => clearInterval(t);
|
||||
}, [imp]);
|
||||
|
||||
if (!imp) return null;
|
||||
return (
|
||||
<div className="sticky top-0 z-[60] bg-amber-400 text-amber-950 px-4 py-2 text-sm flex flex-wrap items-center justify-center gap-x-3 gap-y-1 shadow" role="status">
|
||||
<Eye className="w-4 h-4 shrink-0" />
|
||||
<span><strong>Viewing as {imp.name}</strong> ({imp.email}) — read-only, nothing can be changed.</span>
|
||||
<span className="tabular-nums">{left} left</span>
|
||||
<button
|
||||
onClick={() => { setEnding(true); endImpersonation(); }}
|
||||
disabled={ending}
|
||||
className="px-2.5 py-1 rounded bg-amber-950 text-amber-50 text-xs font-medium hover:bg-black disabled:opacity-60"
|
||||
>
|
||||
{ending ? "Ending…" : "End impersonation"}
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user