skip to Main Content

I was working with jsonwebtoken. Then I go to verify jsonwebtoken I see some tutorial. But when I am using my next () function I am not getting my expected data

I would have benefited from your help. Thanks

// This is for verifying the jsonwebtoken

function verifyJwt(req, res, next) {
  const authHeaders = req.headers.authorization;
  if (!authHeaders) {
    return res.status(401).send({ message: "Unauthorized access" });
  }
  const token = authHeaders.split(" ")[1];
  // verify a token symmetric
  jwt.verify(token, process.env.ACCESS_TOKEN_SECRET, function (err, decoded) {
    if (err) {
      return res.status(403).send({ message: "Forbidden Access" });
    }
    req.decoded = decoded;
    next();
    console.log(decoded);
  });

  console.log(token);
} 
//This one is for giving access if the email matched then it will give me the needed data

app.get("/booking", verifyJwt, async (req, res) => {
      const patientEmail = req.query.patientEmail;
      // Left over
      const decodedEmail = req.decoded.patientEmail;
      if (patientEmail === decodedEmail) {
        const query = { patientEmail: patientEmail };
        const services = await bookingCollection.find(query).toArray();
        // const authorization = req.headers.authorization;
        console.log(authorization);
        return res.send(services);
      } else {
        return res.status(403).send({ message: "Forbidden Access" });
      }
    });

This next() function not working any solve ?

3

Answers


  1. Chosen as BEST ANSWER

    Problem Solved
    Thanks Everyone


  2. Please try the below code

    try {
        const decoded = jwt.verify(token, process.env.ACCESS_TOKEN_SECRET);
        req.decoded = decoded;
        console.log(decoded);
        next();
    } catch (ex) {
        return res.status(403).send({ message: 'Forbidden Access' });
      }
    
    Login or Signup to reply.
  3. appointments.map is not a function. Please console appointment and see what it returns.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search