fastify/jwt 怎么方便的对路由加上认真

查看 49|回复 1
作者:xpyusrs   
import fp from "fastify-plugin";
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

Belmode   
https://fastify.dev/docs/latest/Reference/Middleware/
您需要登录后才可以回帖 登录 | 立即注册

返回顶部