modella-bcrypt

Adds a hashed field to a model using bcrypt-nodejs and a method to verify it. Can be used for password encryption.

Usage no npm install needed!

<script type="module">
  import modellaBcrypt from 'https://cdn.skypack.dev/modella-bcrypt';
</script>

README

modella-bcrypt

Adds a hashed field to a model using bcrypt-nodejs and a method to verify it. Can be used for password encryption.

Installation

npm install modella-bcrypt

Usage

Encrypt field password.

var modella = require('modella');
var encrypter = require('modella-bcrypt');
var User = modella('User');

User.use(encrypter({ fieldName: 'password' }));

User.
  attr('_id').
  attr('email').
  attr('password');

var user = new User({
  email: 'test@example.com',
  password: 'secret'
});

user.save(function () {
  // user.password === [Hashed password]
});

To verify the password of the user:

user.compareField('password', pass, function (err, match) {
  // match === true if verification is correct
});

// or
user.compareFieldSync(pass); // true or false

Options

fieldName

Sets the name of the field to use, defaults to password.

User.use(encrypter({ fieldName: 'pass' });

rounds

Sets the bcrypt rounds. Rounds determine the complexity used for encryption with bcrypt-nodejs (see bcrypt-nodejs docs).

User.use(encrypter({ rounds: 8 });