4

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?

3 Answers 3

13

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);
});
Sign up to request clarification or add additional context in comments.

5 Comments

Silly that it says that all arguments besides progress are required, since it's obviously also required.
@robetklep Ah yes, I am trying, I think when there is only 1 callback, that callback should be the result callback not the progress callback
Yes, I agree. The code doesn't take that into account, sadly.
@robertklep Tested :> How do you think about this API? I see that library has "349 downloads in the last day"
I would agree that when there's only one callback given, it should be the result callback. But I think it might just be an oversight of the developer that it doesn't check for three arguments being passed instead of four; it's probably not intentionally done like this.
1

You can actually use bcyrpt-nodejs if thats what you prefer,but you have to edit the following section in bCrypt.js

if(!callback) {
    throw "No callback function was given."
}

and replace it with

if(typeof callback == 'undefined') {
    callback = progress;
    progress = null;
}

then in your code, just have this;

 require('bcrypt').hash(values.password,null,null,function passwordEncrypted(err,password){

Comments

0

use bcyrpt.compareSync instead of bcypt.compare. It doesn't require a callback

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.