File size: 1,174 Bytes
ceb943f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { auth } from "./auth";
import { headers } from "next/headers";

/**
 * Admin authorization utility
 * 
 * This uses a simple environment variable-based allowlist of emails
 * to authorize ops admins. This avoids modifying the core user schema
 * and allows for quick rotation of admin access.
 */

/**
 * Gets the admin allowlist from environment variables
 */
function getAdminEmails(): string[] {
    return (process.env.ADMIN_EMAILS || "")
        .split(",")
        .map((e) => e.trim().toLowerCase())
        .filter(Boolean);
}

/**
 * Checks if an email is in the admin allowlist
 */
export function isAdminEmail(email: string | null | undefined): boolean {
    if (!email) return false;
    return getAdminEmails().includes(email.toLowerCase());
}

/**
 * Gets the current session and verifies it belongs to an admin
 * Returns the session if valid admin, null otherwise
 */
export async function getAdminSession() {
    const session = await auth.api.getSession({
        headers: await headers(),
    });

    if (!session?.user) {
        return null;
    }

    if (!isAdminEmail(session.user.email)) {
        return null;
    }

    return session;
}