user11880398
user11880398

Reputation:

how check if user exist in nodejs

I'm making a auth system with nodejs and mongoDB(mongoose) and I get Error:

error: UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at F:\tryMern\index.js:68:13

index.js (important part) :

app.post("/api/auth", (req, res) => {
  if (req.body.username && req.body.pass && req.body.status == "new") {
    User.find({ username: req.body.username }, (err, users) => {
      if (!err) {
        if (users.length > 0) {
          return res.json({ error: "The username is taken." });
        }
      }
    });

    const validReq = validate.validate({
      username: req.body.username,
      pass: req.body.pass,
    });

    if (validReq.error) {
      return res.json({ error: validReq.error.details[0].message });
    }
    bcrypt.hash(req.body.pass, 12).then((hashedPass) => {
      // console.log(hashedPass);
      const user = new User({
        username: req.body.username,
        password: hashedPass,
      });
      user.save().then((user) =>
        res.json({
          status: "OK",
          username: user.username,
          token: jwt.sign({ _id: user._id }, jwtKey),
        })
      );
    });

    return;
  }

  User.find({ username: req.body.username }, (err, users) => {
    if (err) {
      console.log(err);
    } else {
      if (users.length > 0) {
        bcrypt.compare(req.body.pass, users[0].password, (err, hash) => {
          if (hash) {
            return res.json({
              validate: true,
              username: users[0].username,
              token: jwt.sign({ _id: users[0]._id }, jwtKey),
            });
          } else {
            return res.json({ validate: false });
          }
        });
      } else {
        return res.json({ validate: false });
      }
    }
  });
});

when I add The username is taken part the error comes ( the part say find user and if its exist say username is taken) if there is another way to check if user exist please tell or fix this problem thanks :)

EDIT: when i try to submit the user with exist username the response is { "error": "The username is taken." } and the error come

Upvotes: 1

Views: 1374

Answers (1)

user11880398
user11880398

Reputation:

I fix this:


app.post("/api/auth", (req, res) => {
  if (req.body.username && req.body.pass && req.body.status == "new") {
    User.find({ username: req.body.username }, (err, users) => {
      if (!err) {
        if (users.length > 0) {
          res.json({ error: "The username is taken." });

          return;
        }
        const validReq = validate.validate({
          username: req.body.username,
          pass: req.body.pass,
        });

        if (validReq.error) {
          return res.json({ error: validReq.error.details[0].message });
        }
        bcrypt.hash(req.body.pass, 12).then((hashedPass) => {
          // console.log(hashedPass);
          const user = new User({
            username: req.body.username,
            password: hashedPass,
          });
          user.save().then((user) =>
            res.json({
              status: "OK",
              username: user.username,
              token: jwt.sign({ _id: user._id }, jwtKey),
            })
          );
        });
      }
    });

    return;
  }

  User.find({ username: req.body.username }, (err, users) => {
    if (err) {
      console.log(err);
    } else {
      if (users.length > 0) {
        bcrypt.compare(req.body.pass, users[0].password, (err, hash) => {
          if (hash) {
            return res.json({
              validate: true,
              username: users[0].username,
              token: jwt.sign({ _id: users[0]._id }, jwtKey),
            });
          } else {
            return res.json({ validate: false });
          }
        });
      } else {
        return res.json({ validate: false });
      }
    }
  });
});

Upvotes: 1

Related Questions