File size: 743 Bytes
6dd9bad 87dcd87 6dd9bad 87dcd87 6dd9bad | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import { FastifyRequest, FastifyReply } from 'fastify';
/**
* Middleware to verify JWT token.
* Throws an error if the token is invalid or missing.
*/
export const verifyJwt = async (request: FastifyRequest, reply: FastifyReply) => {
try {
// EventSource (SSE) cannot send custom headers — accept JWT as ?token= query param fallback
const queryToken = (request.query as Record<string, string>)?.token;
if (queryToken && !request.headers.authorization) {
request.headers.authorization = `Bearer ${queryToken}`;
}
await request.jwtVerify();
} catch (err) {
reply.code(401).send({ error: 'Unauthorized', message: 'Invalid or missing token' });
throw err;
}
};
|