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
okey metin2 pvp serverler metin2 pvp
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat

viagra fiyatı

viagra 100 mg fiyat

viagra sipariş

orjinal viagra

viagra satın al

izmir escort
ataşehir escort
kocaeli escort izmit escort bayan gebze escort izmit sınırsız escort escort bursa tuzla escort aydınlı escort orhanlı escort bursa escort escort bayan istanbul escort istanbul sınırsız escort istanbul bayan escort izmit escort izmit escort kocaeli escort sisli escort mecidiyekoy escort bursa escort bayan bursa görükle escort maltepe escort goztepe escort yakacik-yeni-escort bursa sınırsız escort sisli escort bayan taksim escort bursa escort porno izle sikiş izle atasehir escort antalya escort
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort
bedava bahis