Home > Blockchain >  JWT does not generate hashed passwords
JWT does not generate hashed passwords

Time:12-13

I am trying to do something like this, I want to check if a user exists , and if yes it should show that the user exists and if not, it should go ahead and register the user.

I stumbled into an error. and hence, it does not generate the hashed password and it gives me this very funny error too :

q.png

My code is looking like this

app.post("/api/sign-up", async function (req, res) {
  dbConn.query(
    `select * from accounts where email = ${dbConn.escape(req.body.email)}`,
    function (err, result, fields) {
      if (result.length === 0) {
        var email = req.body.email;
        var phone = req.body.phone;
        var password = req.body.password;
        var fullname = "NULL";

        const hashPass = await bcrypt.hash(password, 12);

        dbConn.query(
          `insert into accounts(email, phone, password, fullname) values (?,?,?,?)`,
          [email, phone, hashPass, fullname],
          function (error, results, fields) {
            if (error) throw error;

            return res.send({
              error: false,
              data: results,
              message: "User created Successfully",
            });
          }
        );
      } else {
        return res.send({
          error: true,
          message: "User exists",
        });
      }
    }
  );
});

If i have to remove the await it does not work as expected. What could I possibly be doing wrongly?

CodePudding user response:

Try to add the async in the callback function:

app.post("/api/sign-up", async function (req, res) {
  dbConn.query(
    `select * from accounts where email = ${dbConn.escape(req.body.email)}`,
    async function (err, result, fields) { // <==== HERE
      if (result.length === 0) {
        var email = req.body.email;
        var phone = req.body.phone;
        var password = req.body.password;
        var fullname = "NULL";

        const hashPass = await bcrypt.hash(password, 12);

        dbConn.query(
          `insert into accounts(email, phone, password, fullname) values (?,?,?,?)`,
          [email, phone, hashPass, fullname],
          function (error, results, fields) {
            if (error) throw error;

            return res.send({
              error: false,
              data: results,
              message: "User created Successfully",
            });
          }
        );
      } else {
        return res.send({
          error: true,
          message: "User exists",
        });
      }
    }
  );
});
  • Related