0

I am receiving an error "data and salt arguments required". Express/node js application using postgresql.

Edit: I think my issue is with passing the 'password' correctly from the client side, because when I console.log(password) it returns undefined + the error mentioned above. I'm not sure how to solve this issue.

Any help or advice would greatly be appreciated.

This is my code for the jwtAuth:

const express = require("express");
const router = require("express").Router();
const bcrypt = require("bcrypt");
const pool = require("../../../db");

router.post("/register", async (req, res) => {

    try { // 
        const {email, password, name1, name2} = req.body;

        const user = await pool.query("SELECT * FROM userr WHERE email = $1", [email]);

        if (user.rows.length !== 0) {
            return res.status(401).send("user already exists"); // this works
        }

      // trouble begins here
        //const saltRound = 10;
        var salt =  await bcrypt.genSalt(10);
        var bcryptPassword =  bcrypt.hashSync(password, salt);

        const newUser = await pool.query("INSERT INTO userr (lastName, firstName, email, passwrd) VALUES ($1, $2, $3, $4) RETURNING *", [name1, name2 , email, bcryptPassword]);
        res.json(newUser.rows[0]);

        // 5. generating jwt token

    } catch (err) {
        console.error(err.message);
        res.status(500).send("server error");
    }
});

module.exports = router;

pengali777
  • 23
  • 1
  • 5

1 Answers1

0

You can use the function version with automatic generation of salt, just passing the saltRounds:

const express = require("express");
const router = require("express").Router();
const bcrypt = require("bcrypt");
const pool = require("../../../db");

router.post("/register", async (req, res) => {
    try {
        const { email, password, name1, name2 } = req.body;

        const user = await pool.query("SELECT * FROM userr WHERE email = $1", [email]);

        if (user.rows.length !== 0) {
            return res.status(401).send("user already exists"); // this works
        }

        const bcryptPassword = await bcrypt.hash(password, 10);
        const newUser = await pool.query(
            "INSERT INTO userr (lastName, firstName, email, passwrd) VALUES ($1, $2, $3, $4) RETURNING *",
            [name1, name2, email, bcryptPassword]
        );

        res.json(newUser.rows[0]);
    } catch (err) {
        console.error(err.message);
        res.status(500).send("server error");
    }
});

module.exports = router;

If you are concerned about the performance of your application, you should not use .hashSync because a synchronous operation will block the thread; instead, it is better to use .hash along with await.

Rustam D9RS
  • 3,236
  • 1
  • 10
  • 17
  • Thank you for your help. However, I have adjusted my code to the one you provided but I am still facing the same error – pengali777 Aug 24 '20 at 18:17
  • 1
    So the problem is not in the code, but in getting the password parameter, please make sure that you correctly send and receive the password parameter from the client. You can output it with `console.log(password);` and see what it outputs. – Rustam D9RS Aug 24 '20 at 18:19
  • So when I console.log(password) it comes out as undefined. That means the clients input for password is not being stored properly (I think). I'm using postman to test it out. How could I store the client password properly? I apologize for asking so many questions !! – pengali777 Aug 24 '20 at 18:59
  • @pengali777 : Check if you have content type in the header, Also `express.json()` for accessing the body – Hidayt Rahman Feb 22 '23 at 11:04