I am using the middleware function to get the user id from the jwt token and it is working completely fine but I want it to send a response when there is no jwt header in the request which it's doing. Here is the code:
const jwt = require("jsonwebtoken");
const fetchuser = (req, res, next) => {
const jwtToken = req.header("jwt-token");
if (!jwtToken) {
console.log(2);
return res
.status(401)
.status({ error: "This action requires the user to login" });
}
try {
const data = jwt.verify(jwtToken, process.env.JWT_SECRET);
if (data.user) {
if (data.user.status === 1) {
req.user = data.user;
} else {
return res.status(500).status({ error: "User account is disabled" });
}
} else {
return res
.status(401)
.status({ error: "This action requires the user to login" });
}
next();
} catch (error) {
return res
.status(401)
.status({ error: "This action requires the user to login" });
}
};
module.exports = fetchuser;
It's definitely going in the first if block as required when the header is absent but it's not sending the response back, the request keeps buffering