/** * Public endpoints: the church's branding for the sign-in pages, the church profile for its * products (`GET /api/church/v1`, contracts 1.5.0), and Connect (`/api/links/v1/*`, as Events 2.4). */ import express, { type Response } from 'express'; import { CHURCH_PATHS, LINK_PATHS, LINK_SCHEMA } from '@crosscode/contracts'; import type { Ctx } from '../context.js'; import { handle, limit } from '../http.js'; import { branding, church } from '../church/profile.js'; import { acceptConnect, acceptDisconnect, LinkError } from '../links/connect.js'; import { setupNeeded } from '../setup/routes.js'; import { currentNotice } from '../privacy/notice.js'; import { DEMO_ASSETS_PATH, demoAsset } from '../demo/scenario.js'; const strip = (p: string) => p.replace(/^\/api/, ''); function linkError(res: Response, e: unknown) { if (e instanceof LinkError) return res.status(e.status).json({ message: e.message, code: e.code }); console.error('[links]', e); return res.status(500).json({ message: 'Something went wrong.', code: 'SERVER_ERROR' }); } export function publicRouter(ctx: Ctx) { const r = express.Router(); const json = express.json({ limit: '16kb' }); r.get('/v2/branding', handle(async () => ({ ...(await branding(ctx)), setupNeeded: await setupNeeded(ctx) }))); // The privacy notice, for the sign-up page's link (public). r.get('/v2/notice', handle(async () => { const n = await currentNotice(ctx); return { version: n.version, title: n.title, body: n.body, publishedAt: n.publishedAt }; })); // 1.9.0: a demo scenario's logo and icon (drawn for the demo), which its church profile points at. r.get(`${strip(DEMO_ASSETS_PATH)}/:scenario/:file`, limit(600), (req, res) => { const svg = demoAsset(String(req.params.scenario), String(req.params.file)); if (!svg) return res.status(404).json({ error: { code: 'NOT_FOUND', message: 'There is no such demo image.' } }); res.set({ 'Content-Type': 'image/svg+xml', 'Cache-Control': 'public, max-age=3600', 'Content-Security-Policy': "default-src 'none'; style-src 'unsafe-inline'" }); return res.send(svg); }); r.get(strip(CHURCH_PATHS.profile), limit(600), handle(async (_req, res) => { res.set('Cache-Control', 'public, max-age=60'); return church(ctx); })); r.post(strip(LINK_PATHS.connect), limit(30), json, async (req, res) => { try { res.json(await acceptConnect(ctx, req.body, req)); } catch (e) { linkError(res, e); } }); r.post(strip(LINK_PATHS.disconnect), limit(30), json, async (req, res) => { try { await acceptDisconnect(ctx, req.body, req); res.json({ schema: LINK_SCHEMA, type: 'link.disconnected' }); } catch (e) { linkError(res, e); } }); return r; }