1.1.0 • Published 6 years ago

mongoose-cryptify v1.1.0

Weekly downloads
10
License
MIT
Repository
github
Last release
6 years ago

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:    'bob@bobross.com',
    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)
1.1.0

6 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.1.1

10 years ago

0.1.0

10 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

11 years ago

0.0.4

11 years ago

0.0.3

11 years ago

0.0.2

11 years ago

0.0.1

11 years ago