Highly ambitious ATProtocol AppView service and sdks
1import type { SessionData } from "./useSession.ts";
2
3/**
4 * Check if the current user is an admin based on VITE_ADMIN_DIDS environment variable.
5 * VITE_ADMIN_DIDS should be a comma-separated list of DIDs.
6 */
7export function isAdmin(session: SessionData | null): boolean {
8 if (!session?.authenticated || !session.user?.did) {
9 return false;
10 }
11
12 const adminDids = import.meta.env.VITE_ADMIN_DIDS;
13 if (!adminDids) {
14 return false;
15 }
16
17 const adminList = adminDids
18 .split(",")
19 .map((did: string) => did.trim())
20 .filter(Boolean);
21
22 return adminList.includes(session.user.did);
23}
24
25/**
26 * Check if the current user is the owner of a slice or an admin.
27 *
28 * @param slice - The slice object with did and actorHandle fields
29 * @param session - The current user session
30 * @returns true if the user is the owner or an admin, false otherwise
31 */
32export function isSliceOwner(
33 slice: { did?: string | null; actorHandle?: string | null } | null | undefined,
34 session: SessionData | null,
35): boolean {
36 if (!session?.authenticated || !session.user) {
37 return false;
38 }
39
40 // Check if user is an admin
41 if (isAdmin(session)) {
42 return true;
43 }
44
45 // Check if user is the slice owner
46 if (!slice) {
47 return false;
48 }
49
50 return (
51 slice.did === session.user.did ||
52 slice.actorHandle === session.user.handle
53 );
54}