/** * Where people sign in (3.0.0, docs/EVENTS-3.0-PLAN.md §5): `local` (Cross Events' own sign-in, as * 2.x, before the move or after Undo) or `accounts` (through the church's Accounts). Kept in * AppSetting `auth_mode`; read with a short cache, so every PM2 process follows a move within * seconds. A new install starts in `accounts` (§9); everyone else starts in `local`. * * Other keys: `auth_moved_at` (ISO, when the move finished: Undo is allowed for 14 days, then the * old secrets are cleared), `auth_move_report` (JSON), `auth_secrets_cleared_at` (ISO). */ /* eslint-disable @typescript-eslint/no-require-imports -- CommonJS */ const prisma = require('../config/db'); /* eslint-enable @typescript-eslint/no-require-imports */ export type AuthMode = 'local' | 'accounts'; export const MODE_KEY = 'auth_mode'; export const MOVED_AT_KEY = 'auth_moved_at'; export const REPORT_KEY = 'auth_move_report'; export const CLEARED_KEY = 'auth_secrets_cleared_at'; export const UNDO_DAYS = 14; const TTL_MS = 5000; let cache: { mode: AuthMode; at: number } | null = null; export async function authMode(): Promise { if (cache && Date.now() - cache.at < TTL_MS) return cache.mode; const row = await prisma.appSetting.findUnique({ where: { key: MODE_KEY } }).catch(() => null); const mode: AuthMode = row?.value === 'accounts' ? 'accounts' : 'local'; cache = { mode, at: Date.now() }; return mode; } export async function viaAccounts(): Promise { return (await authMode()) === 'accounts'; } export async function setSetting(key: string, value: string | null): Promise { if (value === null) await prisma.appSetting.deleteMany({ where: { key } }); else await prisma.appSetting.upsert({ where: { key }, update: { value }, create: { key, value } }); if (key === MODE_KEY) cache = null; } export async function getSetting(key: string): Promise { return (await prisma.appSetting.findUnique({ where: { key } }))?.value ?? null; } /** For tests. */ export function forgetMode(): void { cache = null; }