Friday 26 July 2019

How wrap express-jwt function to handle errors on its own

If I stick expressJwt(...) in route like router.post("/protected", expressJwt, nextFn) it works... But when the expressJwt is on it's own, you have to do error handling in the next function. I want to do it like so.exports.protect = (req, res, next) => { expressJwt({ secret: process.env.JWT_SECRET }) if (!req.user) return res.status(401).json("Unauthorized") return next() } But it doesn't work!Even making one giant function to handle a route and sticking expressJwt at the beginning fails.

Submitted July 27, 2019 at 01:17AM by dittospin

No comments:

Post a Comment