1

I'm not looking for authorization of a particular request and so I did this.If the request path matches I want to skip the auth.verifyToken middleware.How can I do this.I tried using return next() but its not working.

 eventRouter.param('pin', (req, res, next, pin) => {
        let path = `/event/matchpin/${pin}`;
         if(req.path === path){
            //do something here so that directly executes the route
         }
         //else it executes the auth.verifyToken middleware
        next();
      });
    app.use('/user',auth.verifyToken,eventRouter);
Rahul jhawar
  • 227
  • 2
  • 4
  • 16
  • You can add compare check in the middleware itself. Can you post whole code? – Harshal Yeole Jun 09 '18 at 08:57
  • I would just apply the middleware to all routes that need it and not on anything you don't at the router definition level rather than "globally" applying to the endpoint with `use`. See: [Use specific middleware in Express for all paths except a specific one](https://stackoverflow.com/a/12924759/2313887) – Neil Lunn Jun 09 '18 at 09:05
  • @CaptainJackSparrow I cannot because params is not accessible in the middleware – Rahul jhawar Jun 09 '18 at 09:08
  • Fair option @NeilLunn – Harshal Yeole Jun 09 '18 at 09:23

1 Answers1

1

next() is used to skip the middleware, you are just using it at the wrong place.

Try this code:

 eventRouter.param('pin', (req, res, next, pin) => {
        let path = `/event/matchpin/${pin}`;
         if(req.path === path){
            // Next will by pass this middleware
            next();
         }
         //else it executes the auth.verifyToken middleware

      });
    app.use('/user',auth.verifyToken,eventRouter);
Harshal Yeole
  • 4,812
  • 1
  • 21
  • 43