import fastifyJwt from "@fastify/jwt";
const jwtPlugin = fp(async function (fastify, options) {
fastify.register(fastifyJwt, {
secret: "sasdsa312321dasasdas21312ldas", // 加密密钥
});
fastify.decorate("authenticate", async function (request, reply) {
try {
await request.jwtVerify();
} catch (err) {
reply.send(err);
}
});
});
export default jwtPlugin;
fastify.get(
"/user/info",
{ onRequest: [fastify.authenticate] },
userController.info
);
官方推荐的这样做成一个插件, 然后在路由的 onRequest , 这样每个请求都要写, 有啥办法加在一组路由中, 或者给绝大部分的路由都加上
fastify, onrequest, import, fastifyjwt