【问题标题】:bcrypt is not functioning properlybcrypt 无法正常运行
【发布时间】:2021-12-07 16:47:38
【问题描述】:

这里我使用的是 bcrypt 包,但是当我尝试在 Visual Studio 中使用 Thunder 客户端创建用户时,它没有执行散列,密码没有与散列值一起存储,因为我在 const salt 下面编写了代码,并且const secPass.

这是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

这是 package.json 文件:

{
  "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"
  }
}

【问题讨论】:

    标签: javascript node.js reactjs express


    【解决方案1】:

    您正在对密码进行哈希处理,但没有使用它。试试这个。

    
    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
    
    

    【讨论】:

      猜你喜欢
      • 2019-07-10
      • 2021-04-22
      • 2015-12-08
      • 2019-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多