* Build app config object for injection into HTML pages. * This allows nav.js to read config synchronously instead of making an async fetch.
(user?: { id?: string; email: string; firstName?: string | null; lastName?: string | null; isMember?: boolean; isAdmin?: boolean } | null)
| 412 | * This allows nav.js to read config synchronously instead of making an async fetch. |
| 413 | */ |
| 414 | function buildAppConfig(user?: { id?: string; email: string; firstName?: string | null; lastName?: string | null; isMember?: boolean; isAdmin?: boolean } | null) { |
| 415 | // Trust a pre-resolved isAdmin (set by enrichUserWithAdmin / dev-user flag). |
| 416 | // Fall back to ADMIN_EMAILS for callers that haven't enriched yet. |
| 417 | let isAdmin = false; |
| 418 | if (user) { |
| 419 | if (typeof user.isAdmin === 'boolean') { |
| 420 | isAdmin = user.isAdmin; |
| 421 | } else { |
| 422 | const adminEmails = process.env.ADMIN_EMAILS?.split(',').map(e => e.trim().toLowerCase()) || []; |
| 423 | isAdmin = adminEmails.includes(user.email.toLowerCase()); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | return { |
| 428 | authEnabled: AUTH_ENABLED, |
| 429 | user: user ? { |
| 430 | id: user.id, |
| 431 | email: user.email, |
| 432 | firstName: user.firstName, |
| 433 | lastName: user.lastName, |
| 434 | isAdmin, |
| 435 | isMember: !!user.isMember, |
| 436 | } : null, |
| 437 | posthog: POSTHOG_API_KEY ? { |
| 438 | apiKey: POSTHOG_API_KEY, |
| 439 | host: POSTHOG_HOST, |
| 440 | } : null, |
| 441 | }; |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Generate the script tags to inject app config and PostHog into HTML. |
no test coverage detected