How to use bcrypt-nodejs

Comprehensive bcrypt-nodejs code examples:

How to use bcrypt-nodejs.genSaltSync:

43
44
45
46
47
48
49
50
51
52

initialize: function() {
  this.on('saving', function(model,attrs,options){
    if (model.get('authType') === 'local'){
      var password = model.get('password');
      var salt = bcrypt.genSaltSync();
      var hash = bcrypt.hashSync(password);
      model.set('password',hash);
    }
  });

How to use bcrypt-nodejs.genSalt:

26
27
28
29
30
31
32
33
34
35
36
  return insertTempUser(hash, tempUserData, callback);
};*/


// async version of hashing function
/*var myHasher = function(password, tempUserData, insertTempUser, callback) {
  bcrypt.genSalt(10, function(err, salt) {
    bcrypt.hash(password, salt, function(err, hash) {
     console.log("Temp user password is -"+hash);

      return insertTempUser(hash, tempUserData, callback);

How to use bcrypt-nodejs.compareSync:

15
16
17
18
19
20
21
22
23
24
25
    return bcrypt.hashSync(password);
}


exports.checkPassword = async (passBody, passUser) => {
    try {
        return bcrypt.compareSync(passBody, passUser);
    } catch (error) {
        console.log(error);
        return error;
    }

How to use bcrypt-nodejs.genSaltAsync:

40
41
42
43
44
45
46
47
48
49

// normalize email address
self.set('email', self.get('email').toLowerCase().trim());

// salt and hash
return bcrypt.genSaltAsync(10).then(function(salt){
  return bcrypt.hashAsync(self.get('password'), salt, null).then(function(hash){
      self.set('password', hash);
      self.set('salt', salt);
      return;

How to use bcrypt-nodejs.hashAsync:

7
8
9
10
11
12
13
14
15
16
    return
  }

  return bcrypt
    .genSaltAsync(SALT_FACTOR)
    .then(salt => bcrypt.hashAsync(user.password, salt, null))
    .then(hash => {
      user.setDataValue('password', hash)
    })
}

How to use bcrypt-nodejs.compareAsync:

29
30
31
32
33
34
35
36
37
38
    beforeSave: hashPassword
  }
})

User.prototype.comparePassword = function (password) {
  return bcrypt.compareAsync(password, this.password)
}

User.associate = function (models) {
}

How to use bcrypt-nodejs.compare:

12
13
14
15
16
17
18
19
20
21
    }
}

function retrievePassword(password, hash, callback, async=false) {
    if(!async) {
        bcrypt.compare(password, hash, function(err, result) {
            callback(err, result);
        });
    }
    else {

How to use bcrypt-nodejs.hash:

0
1
2
3
4
5
6
7
8
9
const bluebird = require('bluebird');
const bcrypt = bluebird.promisifyAll(require('bcrypt-nodejs'));

function storePassword(password, callback, async=false) {
    if(!async) {
        bcrypt.hash(password, null, null, function(err, hash) {
            callback(err, hash);
        });
    }
    else {

How to use bcrypt-nodejs.hashSync:

44
45
46
47
48
49
50
51
52
53
initialize: function() {
  this.on('saving', function(model,attrs,options){
    if (model.get('authType') === 'local'){
      var password = model.get('password');
      var salt = bcrypt.genSaltSync();
      var hash = bcrypt.hashSync(password);
      model.set('password',hash);
    }
  });
}