Files
crosscode-platform/apps/events/backend/src/accounts/mode.ts
T
joshuaandClaude Opus 5.5 c627c61de1 Cross Events 3.0.0: sign-in through the church's Accounts
Settings -> Sign-in: check, move (everyone into Accounts with their password
hashes and authenticator apps, count/hash check, then switch) and undo for
14 days. Sign-in through auth-client with a one-time handoff to Events' own
token; staff roles stepped up to mfa; sign-out and back-channel logout;
user.updated/export/deleted; admin 2FA reset, unlock and profile changes via
Accounts; /login, /register, /forgot-password forward once moved; Events'
own sign-in endpoints answer SIGN_IN_MOVED. User.accountId, AccountsSignIn.

npm run test:accounts runs Events' code against the real Accounts app (11
tests); 5 new Jest tests. MIGRATING-TO-3.md, plan "Changes while building".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 14:10:30 +02:00

50 lines
2.0 KiB
TypeScript

/**
* 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<AuthMode> {
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<boolean> {
return (await authMode()) === 'accounts';
}
export async function setSetting(key: string, value: string | null): Promise<void> {
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<string | null> {
return (await prisma.appSetting.findUnique({ where: { key } }))?.value ?? null;
}
/** For tests. */
export function forgetMode(): void {
cache = null;
}