Add core server Controller base, auth hook, and buildServer

This commit is contained in:
2026-08-12 20:55:06 +02:00
parent 3f99bce3c6
commit 299375bcd3
4 changed files with 82 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
export function registerAuthHook(
app: FastifyInstance,
secretKey: string,
exemptPaths: string[] = ['/health']
): void {
app.addHook('onRequest', async (request: FastifyRequest, reply: FastifyReply) => {
const path = request.url.split('?')[0];
if (!secretKey || exemptPaths.includes(path)) {
return;
}
const header = request.headers.authorization ?? '';
const token = header.startsWith('Bearer ') ? header.slice('Bearer '.length) : undefined;
if (token !== secretKey) {
reply.code(401).send({ error: 'unauthorized' });
}
});
}