initial commit: add server

This commit is contained in:
2026-02-13 22:43:55 -08:00
commit 5faa57a741
16 changed files with 2882 additions and 0 deletions

22
server/src/auth.ts Normal file
View File

@@ -0,0 +1,22 @@
import type { FastifyRequest } from "fastify";
import { env } from "./env.js";
export function requireAdmin(req: FastifyRequest) {
// If ADMIN_TOKEN isn't set, run in "open" mode (dev).
if (!env.ADMIN_TOKEN) return;
const auth = req.headers.authorization;
if (!auth?.startsWith("Bearer ")) {
const err = new Error("missing bearer token");
// @ts-expect-error attach status
err.statusCode = 401;
throw err;
}
const token = auth.slice("Bearer ".length);
if (token !== env.ADMIN_TOKEN) {
const err = new Error("invalid bearer token");
// @ts-expect-error attach status
err.statusCode = 403;
throw err;
}
}