Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No callback error shown when using bcrypt-node

I am have created a node library file called encrypt.js.

Within that are some functions created using bcrypt-nodejs

var bcrypt = require('bcrypt-nodejs');

exports.cryptPassword = function(password, callback) {
    bcrypt.genSalt(10, function(err, salt) {
        if (err) return callback(err);
        else {
            bcrypt.hash(password, salt, function(err, hash) {
                return callback(err, hash);
            });
        }
    });
};

exports.comparePassword = function(password, userPassword, callback) {
    bcrypt.compare(password, userPassword, function(err, isPasswordMatch) {
        if (err) return callback(err);
        else return callback(null, isPasswordMatch);
    });
};

When I now use cryptPassword from my server.js file it shows an error coming from the bcrypt-nodejs library stating 'no callback function was given'

I have added a function within my call as below

var encryptedPassword =  encrypt.cryptPassword(req.body.user.password, function (err, salt){
    if(err) {throw err};
    console.log('hlllll');
});

Can anyone help?

like image 491
tjhack Avatar asked Dec 08 '22 10:12

tjhack


1 Answers

Syntax: bcrypt.hash(data, salt, progress, cb)

You must have two callbacks.

Document here:

https://npmjs.org/package/bcrypt-nodejs


Update:

You can use the package bcrypt instead of bcrypt-nodejs

And your code will work:

bcrypt.hash(password, salt, function(err, hash) {
   return callback(err, hash);
});
like image 136
damphat Avatar answered Dec 11 '22 00:12

damphat