yashrith
yashrith

Reputation: 43

bcrypt is not functioning properly

this is the thunder client you can see at the password field it is not hashedHere I am using bcrypt package but when I try to create user with thunder client at visual studio it is not performing hashing, the password is not storing with hash value as I have written the code below const salt and const secPass.

This is nodejs, expresjs:

const express = require('express');
const router = express.Router();
const User = require('../models/User');
const { body, validationResult } = require('express-validator');
const bcrypt = require('bcryptjs');


router.post('/',[
    body('name').isLength({ min: 3 }),
    body('email').isEmail(),
    body('password').isLength({ min: 5 })
    
] ,async(req,res)=>{
  //if there are any errors it sends bad request 400
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }
    //check wether user exists with this email already
    try{
let user = await User.findOne({email: req.body.email});
if(user){

  return res.status(400).json({error: 'sorry user with this email already exists'})
}

const salt = await bcrypt.genSalt(10);
const secPass = await bcrypt.hash(req.body.password, salt) 

    user = await User.create({
        name: req.body.name,
        email: req.body.email,
        password: req.body.password
      })
      //this is for understanding purpose of other code
    //   .then(user => res.json(user))
    //   .catch(err =>{console.log(err)
    // res.json({error: 'email already exists', message: err.message})});
    res.json(user)
    } 
catch (error){
console.error(error.message);
res.status(500).send('some error occured')
}
})

module.exports = router

This is package.json file:

{
  "name": "my-app",
  "version": "1.0.0",
  "description": "project contains backend and front end",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start-nodemon": "nodemon ./index.js"
  },
  "author": "yashrith",
  "license": "ISC",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "express": "^4.17.1",
    "express-validator": "^6.13.0",
    "mongoose": "^6.0.13"
  },
  "devDependencies": {
    "nodemon": "^2.0.15"
  }
}

Upvotes: 1

Views: 2297

Answers (1)

Youssouf Oumar
Youssouf Oumar

Reputation: 46261

You are hashing your password but not using it. Try with this.


const express = require('express');
const router = express.Router();
const User = require('../models/User');
const { body, validationResult } = require('express-validator');
const bcrypt = require('bcryptjs');


router.post('/',[
    body('name').isLength({ min: 3 }),
    body('email').isEmail(),
    body('password').isLength({ min: 5 })
    
] ,async(req,res)=>{
  //if there are any errors it sends bad request 400
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }
    //check wether user exists with this email already
    try{
let user = await User.findOne({email: req.body.email});
if(user){

  return res.status(400).json({error: 'sorry user with this email already exists'})
}

const salt = await bcrypt.genSalt(10);
const secPass = await bcrypt.hash(req.body.password, salt) 

    user = await User.create({
        name: req.body.name,
        email: req.body.email,
        password: secPass
      })
      //this is for understanding purpose of other code
    //   .then(user => res.json(user))
    //   .catch(err =>{console.log(err)
    // res.json({error: 'email already exists', message: err.message})});
    res.json(user)
    } 
catch (error){
console.error(error.message);
res.status(500).send('some error occured')
}
})

module.exports = router

Upvotes: 1

Related Questions