How can I get the hashed password to pass to my database?


Howie

I can run the tests in Postman, but for some reason I can't pass the hashed password into the database correctly.

const express = require("express");
// const helmet = require("helmet");
const { User } = require("./db/models");
const bcrypt = require("bcryptjs");
var salt = bcrypt.genSaltSync(10);
var hash = bcrypt.hashSync('bacon', 8);

const port = 4000;

const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true, limit: "50mb" }));

// app.use(helmet());

app.get("/", (req, res) => {
  res.send("Hello World!");
});

I suspect this is the problem. I am passing name, email and password. I'm trying to figure out how to get the hash to pass into the database as a password.

app.post("/register", async (req, res) => {
  const user = await User.create({name:req.body.name, email:req.body.email, password:req.body.password});

  bcrypt.genSalt().then(salt => {
    bcrypt.hash("password", salt).then(hash =>{
      console.log(hash);
    });
  })

  console.log(req.body.name)
  res.json(user);
 
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});
Girish Srivatsa

Since you are in an async function, you can use await to get the value before the User object is created

app.post("/register", async (req, res) => {
  const salt = await bcrypt.genSalt();
  const hashed_password = await bcrypt.hash(req.body.password,salt);
  const user = await User.create({name:req.body.name, email:req.body.email, password:hased_password});
  console.log(req.body.name)
  res.json(user);
});

Related


How can I get the hashed password to pass to my database?

Howie I can run the tests in Postman, but for some reason I can't pass the hashed password into the database correctly. const express = require("express"); // const helmet = require("helmet"); const { User } = require("./db/models"); const bcrypt = require("bc

How can I get the hashed password to pass to my database?

Howie I can run the tests in Postman, but for some reason I can't pass the hashed password into the database correctly. const express = require("express"); // const helmet = require("helmet"); const { User } = require("./db/models"); const bcrypt = require("bc

How can I get the hashed password to pass to my database?

Howie I can run the tests in Postman, but for some reason I can't pass the hashed password into the database correctly. const express = require("express"); // const helmet = require("helmet"); const { User } = require("./db/models"); const bcrypt = require("bc

How can I get the hashed password to pass to my database?

Howie I can run the tests in Postman, but for some reason I can't pass the hashed password into the database correctly. const express = require("express"); // const helmet = require("helmet"); const { User } = require("./db/models"); const bcrypt = require("bc

Django - how to save my hashed password

Brian Cass I'm trying to save a hashed password in a database but it keeps saving my plain text password role model: class StudentRegistration(models.Model): email = models.EmailField(max_length=50) first_name = models.CharField(max_length=20) last

How does the application read my hashed password?

reg mpdas (last.fm scrobbler for mpd) forces a write password hashed in md5. But if it's hashed, how does it log into last.fm? Aren't hash functions one-way? Scott Chamberlain Because last.fm doesn't store your password, they only store the md5 hash of your pa

How does the application read my hashed password?

reg mpdas (last.fm scrobbler for mpd) forces a write password hashed in md5. But if it's hashed, how does it log into last.fm? Aren't hash functions one-way? Scott Chamberlain Because last.fm doesn't store your password, they only store the md5 hash of your pa