Files
crosscode-platform/apps/accounts/backend/src/app.ts
T
joshuaandClaude Opus 5.5 733992e917 Accounts 1.1.0: moving Events' users in, people.* requests, new recovery codes
POST /api/accounts/v1/requests (crosscode.identity-request/1): import.* keeps
each person's Events id as sub, password hash and authenticator secret
(sealed in transit), consent, notices and lockout; linked when the email
already has an account. people.* for product admins, limited to people the
product knows. New recovery codes after the first 2FA sign-in for people
moved in with 2FA. screen=signup. auth-client: request(), seal(), signUp.
8 new tests (47 in all).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 13:40:15 +02:00

72 lines
3.2 KiB
TypeScript

/**
* The Express app (docs/ACCOUNTS-1.0-PLAN.md §1). Everything is under /api, so the site and its API
* share one address (as Events 2.2): nginx sends /api to this backend and the rest to Next.js.
*
* /api/health version and database, for monitoring and the Hub
* /api/oidc/… the OpenID Connect provider (discovery, auth, token, …)
* /api/self/… the account page's own sign-in
* /api/church/v1, /api/links/v1/… for the church's other products
* /api/accounts/v1/requests what a linked product asks (import, people.*; contracts 1.8.0)
* /api/v2/… the pages' API (sign-in steps, account, admin, setup)
*/
import express from 'express';
import type Provider from 'oidc-provider';
import type { Ctx } from './context.js';
import { sameOrigin, sendError } from './http.js';
import { adminRouter } from './admin/routes.js';
import { interactionRouter, selfRouter } from './interaction/routes.js';
import { meRouter } from './me/routes.js';
import { LOGOUT_JS } from './oidc/pages.js';
import { publicRouter } from './public/routes.js';
import { requestsRouter } from './links/requests.js';
import { setupRouter } from './setup/routes.js';
import { health } from './hub/client.js';
import { appVersion } from './lib/version.js';
export function createApp(ctx: Ctx, provider: Provider) {
const app = express();
app.disable('x-powered-by');
if (ctx.config.trustProxySetting !== false) app.set('trust proxy', ctx.config.trustProxySetting);
app.use((_req, res, next) => {
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('Referrer-Policy', 'same-origin');
res.setHeader('X-Frame-Options', 'DENY');
res.setHeader('X-Robots-Tag', 'noindex, nofollow');
next();
});
app.get('/api/health', async (_req, res) => {
const h = await health(ctx);
res.status(h.db ? 200 : 503).json({ status: h.db ? 'ok' : 'error', product: 'accounts', version: appVersion(), db: h.db });
});
app.get('/api/static/logout.js', (_req, res) => {
res.type('application/javascript').set('Cache-Control', 'public, max-age=3600').send(LOGOUT_JS);
});
// The provider parses its own bodies: mounted before express.json.
app.use('/api/oidc', provider.callback());
app.use('/api/self', selfRouter(ctx));
app.use('/api', requestsRouter(ctx, provider));
app.use('/api', publicRouter(ctx));
const v2 = express.Router();
v2.use(express.json({ limit: '64kb' }));
v2.use(sameOrigin(ctx));
v2.use(interactionRouter(ctx, provider));
v2.use(setupRouter(ctx));
v2.use(meRouter(ctx, provider));
v2.use('/admin', adminRouter(ctx, provider));
v2.use((_req, res) => res.status(404).json({ error: { code: 'NOT_FOUND', message: 'There is no such API endpoint.' } }));
app.use('/api/v2', v2);
app.use((err: unknown, _req: express.Request, res: express.Response, next: express.NextFunction) => {
if (res.headersSent) return next(err);
const e = err as { type?: string };
if (e?.type === 'entity.parse.failed') return res.status(400).json({ error: { code: 'VALIDATION', message: 'The body must be JSON.' } });
return sendError(res, err);
});
return app;
}