tariffs/src/handlers/auth/middleware.ts

20 lines
505 B
TypeScript
Raw Normal View History

2022-12-20 15:07:06 +00:00
import type { FastifyRequest, FastifyReply, HookHandlerDoneFunction as Done } from "fastify";
export const verifyUser = async (request: FastifyRequest, reply: FastifyReply, done: Done) => {
try {
const { id } = await request.jwtVerify<{ id?: string }>({ onlyCookie: true });
if (!id) {
reply.status(401);
return reply.send("user id is empty");
}
request.user = { id };
} catch (nativeError) {
reply.status(401);
return reply.send(nativeError);
}
done();
};