text: import jwt from "jsonwebtoken";
  
  export default (req, res, next) => {
    if (!req.get("Authorization")) {
      const error = new Error("No authorization header.");
      error.statusCode = 401;
      throw error;
    }
    const token = req.get("Authorization").split(" ")[1];
    let decodedToken;
    try {
      decodedToken = jwt.verify(token, process.env.JWT_SECRET);
    } catch (err) {
      err.message = "Could not verify the token.";
      err.statusCode = 500;
      throw err;
    }
    if (!decodedToken) {
      const error = new Error("Not authenticated.");
      error.statusCode = 401;
      throw error;
    }
    req.userId = decodedToken.userId;
    next();
  };