How to fix Data and hash arguments required error

Viewed 10

I'm trying to let the user sign in using either email or username, I'm trying this code in Postman and each time I try it gives me this error:

"success":false,"status":500,"message":"data and hash arguments required","stack":"Error: data and hash arguments required\n at Object.compare

Auth.js Configurations:

const emailRegex = "/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/";
router.post("/login", async (req, res, next) => {

  const isEmail = String(req.body.emailOrUsername).match(emailRegex);
  try {

    if (!req.body.password) {
      return next(createError(400, "Invalid password"));
    }

    let user;
    
    if (isEmail) {
      user = await User.findOne({ where: { email: req.body.emailOrUsername } });
    } else {
      user = await User.findOne({ where: { username: req.body.emailOrUsername } });
    };
    
    if (!user) return next(createError(404, "User not found!"));

    const isPasswordCorrect = await bcrypt.compare(
      req.body.password,
      user.password
    );
    
    if (!isPasswordCorrect) return next(createError(400, "Wrong password!"));
    
    const { password, ...others } = user._doc;
    
    res.status(200).json(others);
  } catch (err) {
    next(err);
  }

});

I'm not sure what I'm missing here!

0 Answers
Related