Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JWT does not generate hashed passwords

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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",
        });
      }
    }
  );
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading