Skip to content
File

Blob: src/client/hooks/useInboxRouteSession.ts

typescript41 lines
1import { useMemo } from "react";
2import { useParams, useSearchParams } from "react-router-dom";
3import { getInboxSession, type AuthDescriptor } from "@/client/lib/api";
4 
5interface InboxRouteSession {
6 address: string;
7 adminMode: boolean;
8 auth: AuthDescriptor | null;
9}
10 
11export function useInboxRouteSession(): InboxRouteSession {
12 const params = useParams();
13 const [searchParams] = useSearchParams();
14 
15 const address = useMemo(() => decodeURIComponent(params.address ?? ""), [params.address]);
16 // Admin inspection is selected by explicit `?admin=1` only. The cookie
17 // backing the admin session is httpOnly so the client cannot detect it
18 // locally; admin pages link to inbox routes with `?admin=1` already, so
19 // the only way to land in admin mode is through that explicit hint.
20 const adminMode = searchParams.get("admin") === "1";
21 
22 if (!address) {
23 return { address, adminMode, auth: null };
24 }
25 
26 if (adminMode) {
27 return { address, adminMode, auth: { mode: "admin" } };
28 }
29 
30 const userSession = getInboxSession(address);
31 if (!userSession) {
32 return { address, adminMode, auth: null };
33 }
34 
35 return {
36 address,
37 adminMode,
38 auth: { mode: "user", token: userSession.token },
39 };
40}