score: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.


More questions

More questions with similar tag