V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
xpyusrs
V2EX  ›  Node.js

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

  •  
  •   xpyusrs · Dec 14, 2023 · 2177 views
    This topic created in 865 days ago, the information mentioned may be changed or developed.
    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 replies    2023-12-15 11:15:48 +08:00
    xpyusrs
        2
    xpyusrs  
    OP
       Dec 14, 2023
    @Belmode 这个中间件是设置全部路由的中间件吧!有啥办法设置一组路由的
    Belmode
        3
    Belmode  
       Dec 15, 2023
    通配符模式
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1234 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:34 · PVG 07:34 · LAX 16:34 · JFK 19:34
    ♥ Do have faith in what you're doing.