api/src/auth/middleware.tsblame
View source
3e3af551import type { FastifyRequest, FastifyReply } from "fastify";
3e3af552
3e3af553/**
3e3af554 * Auth middleware — verifies JWT and attaches user to request
3e3af555 */
3e3af556export async function authenticate(
3e3af557 request: FastifyRequest,
3e3af558 reply: FastifyReply
3e3af559) {
3e3af5510 try {
3e3af5511 await request.jwtVerify();
3e3af5512 } catch {
3e3af5513 reply.code(401).send({ error: "Unauthorized" });
3e3af5514 }
3e3af5515}
3e3af5516
3e3af5517/**
3e3af5518 * Optional auth — attaches user if present, but doesn't reject
3e3af5519 */
3e3af5520export async function optionalAuth(
3e3af5521 request: FastifyRequest,
3e3af5522 _reply: FastifyReply
3e3af5523) {
3e3af5524 try {
3e3af5525 await request.jwtVerify();
3e3af5526 } catch {
3e3af5527 // Not authenticated, but that's OK for public routes
3e3af5528 }
3e3af5529}