text: const userModel = require("../../models/users");
  const crypto = require("crypto");
  
  exports.hasAuthValidFields = (req, res, next) => {
    let errors = [];
  
    if (req.body) {
      if (!req.body.email) {
        errors.push("Missing email field");
      }
      if (!req.body.password) {
        errors.push("Missing password field");
      }
  
      if (errors.length) {
        return res.status(400).send({ errors: errors.join(",") });
      } else {
        return next();
      }
    } else {
      return res
        .status(400)
        .send({ status: "failed", message: "Missing email and password fields" });
    }
  };
  
  exports.isPasswordAndUserMatch = (req, res, next) => {
    userModel.findByEmail(req.body.email).then((user) => {
      if (user.length !== 1) {
        res
          .status(404)
          .send({
            message: "User not found. Please register first",
            status: "failed",
          });
      } else {
        let passwordFields = user[0].password.split("$");
        let salt = passwordFields[0];
        let hash = crypto
          .createHmac("sha512", salt)
          .update(req.body.password)
          .digest("base64");
        if (hash === passwordFields[1]) {
          req.body = {
            userId: user[0]._id,
            email: user[0].email,
            permissionLevel: user[0].permissionLevel,
            provider: "email",
            name: user[0].firstName + " " + user[0].lastName,
          };
          return next();
        } else {
          return res.status(400).send({
            status: "failed",
            message: "Invalid Email address or password",
          });
        }
      }
    });
  };
  