Skip to content

Latest commit

 

History

History
78 lines (57 loc) · 1.67 KB

readme.md

File metadata and controls

78 lines (57 loc) · 1.67 KB

mongoose-cryptify

A mongoose.js plugin for encrypting schema paths via bcrypt.

Installation

$ npm install mongoose-cryptify --save

Usage

Mongoose plugin style.

var mongoose = require('mongoose'),
    Schema   = mongoose.Schema,
    cryptify = require('mongoose-cryptify'),

var userSchema = new Schema({
  name: {
    first: String,
    last:  String
  },

  login: {
    email:    { type: String, unique: true },
    password: String
  }
});

// Attach some mongoose hooks
userSchema.plugin(cryptify, {
  paths: [ 'login.password' ], // Array of paths
  factor: 10                   // Bcrypt work factor
});

module.exports = mongoose.model('User', userSchema);

And then use bcrypt to compare the hash

var bcrypt = require('bcrypt'),
    User   = require('path/to/model');

var document = new User({
  name: {
    first: 'Bob',
    last:  'Ross'
  },

  login: {
    email:    '[email protected]',
    password: 'nicetree'
  }
});

document.save(function ( err, doc ) {
  if( err ) throw err;

  console.log(doc.login.password); // Some hashsum like $2a$10$lx8X2e2vIiapMwv4DqixJurDnqV8qn7W6Q7ocXygHGD9dp5kEspnm

  bcrypt.compare('nicetree', doc.login.password, function ( err, result ) {
    if( err ) throw err;

    console.log(result); // true
  })
});

See bcrypt's repo for more details on work factor & comparing.

Options

There are only two options used in mongoose-cryptify

  • options.paths {Array} (Required) Array of paths to encrypt
  • options.factor {Number} (Optional) Bcrypt work factor (rounds) (More info here)