Hashing passwords with Bcrypt and node.js

Hashing passwords with Bcrypt and node.js

In my last tutorial, I had explained how to register users and authenticate a user with their password without using any encryption layer but that was not good practice to store password in the table.

In this tutorial, I will tell you how to use basic encryption layer to store password using bcrypt module in Node.js

This is the proper way to save password in the database using bcrypt module.

There are 2 ways to hash the password - sync and async by using the bcrypt module.

Make sure you are using a stable version of node because the module does not support unstable versions.

To use the bcrypt module in Node.js, install it via NPM.

$ npm install bcrypt --save

Once you have installed the bcrypt module, include it in your node application.

// require the bcrypt module
var bcrypt = require('bcrypt');
Synchronous Usase

First generate the salt and then hash the password with the salt.

var salt = bcrypt.genSaltSync(10);
var hash = bcrypt.hashSync(req.body.password, salt);
-- OR --
var hash = bcrypt.hashSync(req.body.password, 10);

To authenticate the incoming password string with the hash stored in the database :

bcrypt.compareSync(req.body.password, hash); 

If requested password match with the hash password then compareSync will return true.

Asynchronous Usase

You can go with Asynchronous method in following way :

bcrypt.hash(req.body.password, 10, function(err, hash) {
    // Store hash password in your Database.
});

To compare the requested password with database password, you can use following line of code :

bcrypt.compare(req.body.password, hash, function(err, res) {
    // res == true
});

A complete example to authenticate password from hash password :

  1. module.exports.authenticate=function(req,res){
  2.     var email=req.body.email;
  3.     var password=req.body.password;
  4.     connection.query('SELECT * FROM users WHERE email = ?',[email], function (error, results, fields) {
  5.      if (error) {
  6.          res.json({
  7.             status:false,
  8.             message:'there are some error with query'
  9.             })
  10.      }else{
  11.      if(results.length >0){
  12.          bcrypt.compare(password, results[0].password, function(err, ress) {
  13.             if(!ress){
  14.                 res.json({
  15.                      status:false,        
  16.              message:"Email and password does not match"
  17.              });
  18.             }else{                 
  19.                     res.json({
  20.                         status:true,
  21.                         message:"Successfully Login"
  22.                     })
  23.             }
  24.             });    
  25.      }
  26.      else{
  27.      res.json({
  28.          status:false,
  29.      message:"Email does not exits"
  30. });
  31.      }
  32.      }
  33.     });
  34. }

Phone: (+91) 8800417876
Noida, 201301
Betwinner
pendik escort
kartal escort