POSTMAN keeps on sending request no response is getting back.(keeps loading)

My question:

I have been creating a node backend webtoken. When I tested it through POSTMAN API. It’s showing “Sending Request” and no result are returning. The code is given below.
Details (like screenshots):

const jwt = require("jsonwebtoken");

const verifyToken = (req, res, next) => {
  const authHeader = req.headers.token;
  if (authHeader) {
    const token = authHeader.split(" ")[1];
    jwt.verify(token, process.env.JWT_SEC, (err, user) => {
      if (err){

        
      res.status(403).json("Token is not valid!");
      req.user = user;
      next();
      } 
    });
  } else {
    return res.status(401).json("YOu are not authenticated!");
  }
};
const verifyTokenAndAuthorization = (req, res, next) => {
  verifyToken(req, res, () => {
    if (req.user.id === req.params.id || req.user.isAdmin) {
      next();
    } else {
      res.status(403).json("You are not allowed to do that!");
    }
  });
};

module.exports = { verifyToken,verifyTokenAndAuthorization };
1 Like

It’s difficult to debug this without more context of how this code is being called, etc, to isolate that this code is causing the problem. Can you add more console.log calls around your code, especially in these methods, to make sure the code execution is even getting to this point?

const jwt = require("jsonwebtoken");

const verifyToken = (req, res, next) => {
  const authHeader = req.headers.token;
  if (authHeader) {
    const token = authHeader.split(" ")[1];
    jwt.verify(token, process.env.JWT_SEC, (err, user) => {
      if (err)res.status(403).json("Token is not valid!");
      req.user = user;
      next();
      
    });
  } else {
    return res.status(401).json("YOu are not authenticated!");
  }
};
const verifyTokenAndAuthorization = (req, res, next) => {
  verifyToken(req, res, () => {
    if (req.user.id === req.params.id || req.user.isAdmin) {
      next();
    } else {
      res.status(403).json("You are not allowed to do that!");
    }
  });
};

module.exports = { verifyToken,verifyTokenAndAuthorization };