tariffs/src/handlers/auth/middleware.ts

23 lines
681 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) => {
2023-05-12 04:07:58 +00:00
console.info("---------------------------verifyUser------------------------------");
2022-12-20 15:07:06 +00:00
try {
2022-12-22 10:45:44 +00:00
const { id } = await request.jwtVerify<{ id?: string }>();
2022-12-20 15:07:06 +00:00
if (!id) {
reply.status(401);
return reply.send("user id is empty");
}
request.user = { id };
} catch (nativeError) {
reply.status(401);
2023-05-12 04:07:58 +00:00
console.info("---------------------------verifyUser error------------------------------", nativeError);
2022-12-20 15:07:06 +00:00
return reply.send(nativeError);
}
done();
};