0

This is my code.

router.route("/login").post((req, res) => {
  const email = req.body.email;
  const password = req.body.password;
  Account.find().then((account) => {
    account.forEach((eachAcc) => {
      if (eachAcc.email.toUpperCase() === email.toUpperCase()) {
        bcrypt.compare(password, eachAcc.password, function (err, result) {
          if (result == true) res.status(200).json("Login Successful");
          else if (err) res.status(400).json("Error: " + err);
        });
      }
    });
    res.status(400).json("Invalid email or password");
  });
});

But I am always getting Invalid email/ password, I want it to be printed only when loop has completed and email/ password didn't match.

Please help.

sandy
  • 459
  • 1
  • 5
  • 18
  • Invalid email/ password should only displayed if there is at least one error 400 status? – Sascha Aug 09 '20 at 18:35
  • 1
    Avoid using `if (result == true)`, either use `if (result)` or `if (result === true)`. https://stackoverflow.com/a/15394130/6451036 – Aroic Aug 09 '20 at 18:38
  • What is your intention of using `Account.find()` here? You need a function as a parameter to test a value. Calling it with no param will return nothing from what I understand. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find – Aroic Aug 09 '20 at 18:46
  • @Sascha yes error message should be displayed with atleast one error – sandy Aug 10 '20 at 03:02
  • @Aroic here Accound.find() is a mongoose query to find all the accounts in my db. https://mongoosejs.com/docs/api.html#model_Model.find – sandy Aug 10 '20 at 03:05

3 Answers3

1

Try this

    router.route("/login").post((req, res) => {
      const { email, password } = req.body;
      Account.find().then((account) => {
      let acc = account.find(
         (eachAcc) => eachAcc.email.toUpperCase() === 
            email.toUpperCase()
         );
      if (acc) {
         bcrypt.compare(password, acc.password, function (err, result) 
          {
            if (err) res.status(400).json("Error: " + err);
            else if (result) res.status(200).json("Login Successful");
            else res.status(400).json("Invalid password");
          });
         } else res.status(400).json("Invalid email");
       });
    });
sandy
  • 459
  • 1
  • 5
  • 18
Sven.hig
  • 4,315
  • 2
  • 5
  • 18
0

You can use setTimeout function for this setTimeout(()=>{//code here},300).

Amit Shakya
  • 748
  • 3
  • 8
  • 25
0

I hope it will help:

router.route("/login").post((req, res) => {
  const { email, password } = req.body;

  Account.find().then((accounts) => {
    let match = false;
    for (acct of accounts) {
      if (acct.email.toUpperCase() !== email.toUpperCase()) continue;

      bcrypt.compare(password, acct.password, (err, result) => {
        if (err) {
          res.status(400).json("Error: " + err);
          break;
        }
        res.status(200).json("Login Successful");
        match = true;
        break;
      });

    }
    if (!match) res.status(400).json("Invalid email or password");
  });
});
Deepak Dixit
  • 1,262
  • 11
  • 22