V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xpyusrs
V2EX  ›  Node.js

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

  •  
  •   xpyusrs · 135 天前 · 1038 次点击
    这是一个创建于 135 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 , 这样每个请求都要写, 有啥办法加在一组路由中, 或者给绝大部分的路由都加上

    3 条回复    2023-12-15 11:15:48 +08:00
    xpyusrs
        2
    xpyusrs  
    OP
       134 天前
    @Belmode 这个中间件是设置全部路由的中间件吧!有啥办法设置一组路由的
    Belmode
        3
    Belmode  
       134 天前
    通配符模式
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2821 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:03 · PVG 18:03 · LAX 03:03 · JFK 06:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.