text: const jwt = require("jsonwebtoken");
  
  // set token secret and expiration date
  const secret = "mysecretsshhhhh";
  const expiration = "2h";
  
  module.exports = {
    // function for our authenticated routes
    authMiddleware: function ({req}) {
     //added code
      let token = req.body.token || req.query.token || req.headers.authorization;
  
      // ["Bearer", "<tokenvalue>"]
      if (req.headers.authorization) {
        token = token.split(" ").pop().trim();
      }
  
      if (!token) {
        // return res.status(400).json({ message: "You have no token!" });
        return req;
      }
  
      // verify token and get user data out of it
      try {
        const { data } = jwt.verify(token, secret, { maxAge: expiration });
        req.user = data;
      } catch {
        console.log("Invalid token");
        // return res.status(400).json({ message: "invalid token!" });
      }
      return req;
      // send to next endpoint
      // next();
    },
    signToken: function ({ username, email, _id }) {
      const payload = { username, email, _id };
      console.log(payload);
      return jwt.sign({ data: payload }, secret, { expiresIn: expiration });
    },
  };
  