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;