1

I am trying to use a middleware to verify my user token. The problems come when I am trying to use next() to move on to the next middle were. What I think is that somehow my next() is not working as it should. Please help.

Middleware code:

async function verify(req,res,next){

    if(req.body.token==null) next("Tokken Not present")
    // verify token
    try{

        var isAuth=jwt.verify(req.body.token,process.env.secretKey);
       if(isAuth)next();
    }catch(err){
        next(err)
    }

}

error msg:

> db connected 
>err handler reached 
>SyntaxError: Illegal break statement
>     at Layer.handle [as handle_request] (F:\recuritmentPortal\backend\node_modules\express\lib\router\layer.js:95:5)
>     at next (F:\recuritmentPortal\backend\node_modules\express\lib\router\route.js:138:13)
>     at verify (F:\recuritmentPortal\backend\middlewares\userVer.js:12:8)
>     at Layer.handle [as handle_request] (F:\recuritmentPortal\backend\node_modules\express\lib\router\layer.js:95:5)
>     at next (F:\recuritmentPortal\backend\node_modules\express\lib\router\route.js:138:13)
>     at Route.dispatch (F:\recuritmentPortal\backend\node_modules\express\lib\router\route.js:112:3)
>     at Layer.handle [as handle_request] (F:\recuritmentPortal\backend\node_modules\express\lib\router\layer.js:95:5)
>     at F:\recuritmentPortal\backend\node_modules\express\lib\router\index.js:281:22
>     at Function.process_params (F:\recuritmentPortal\backend\node_modules\express\lib\router\index.js:335:12)
>     at next (F:\recuritmentPortal\backend\node_modules\express\lib\router\index.js:275:10)
>     at Function.handle (F:\recuritmentPortal\backend\node_modules\express\lib\router\index.js:174:3)
>     at router (F:\recuritmentPortal\backend\node_modules\express\lib\router\index.js:47:12)
>     at Layer.handle [as handle_request] (F:\recuritmentPortal\backend\node_modules\express\lib\router\layer.js:95:5)

1 Answers1

0
    async function verify(req,res,next){

    if(req.body.token==null) 
        return next("Tokken Not present")
    // verify token
    try{

        var isAuth=jwt.verify(req.body.token,process.env.secretKey);
       if(isAuth)next();
    }catch(err){
        next(err)
    }

}

    app.use((error, request, response, next) => {
    response.status(500).send('error') 
})
  • Allan thanks for answering but i am using express router and even if do router.use it still doesnt work. – Pankaj Shukla Jun 14 '20 at 02:06
  • Welcome on stackoverflow! Can you provide any limitations, assumptions or simplifications in your answer. See more details on how to answer at this link: https://stackoverflow.com/help/how-to-answer – Usama Abdulrehman Jun 14 '20 at 02:41