Git Product home page Git Product logo

mongoose-pass's Introduction

mongoose-pass

io.js compatibility NPM version

Dependency Status Dependency Status Code Climate Build Status Coverage Status

Another mongoose password hashing module.

Usage

'use strict';

var mongoose = require('mongoose');
var Schema   = mongoose.Schema;

var MySchema = new Schema({
  username: {
    type: String,
    required: true,
    index: {
      unique: true
    }
  }
});

MySchema.plugin(require('mongoose-pass'));

MySchema.static('authenticate', function (username, password, cb) {
  // Promise version
  return User.findOne({username: username}).exec().then(function (user) {
    if (!user) { return false; }
    return user.authenticate(password);
  });
  // Callback version
  User.findOne({username: username}, function (err, user) {
    if (err) { return cb(err); }
    if (!user) { return cb(null, false); }
    user.authenticate(password, function (err, isMatch) {
      if (err) { return cb(err); }
      cb(null, isMatch);
    });
  });
});

To pass in options:

MySchema.plugin(require('mongoose-pass'), {
  passwordPath: 'password',
  authMethod: 'authenticate',
  saltWorkFactor: 10
});

Note in the example above that it added an authenticate() method. This method is an instance method. Once you have a model, you call this method passing in the tentative password as the first parameter. Now, you can either pass in a callback as the second parameter, or it returns a promise. See examples of both above.

Options

  • passwordPath (String) - The path to add the password property to. Default: 'password'
  • authMethod (String) - The name of the instance method that authenticates a user by password. Default 'authenticate'
  • saltWorkFactor (Number) - The salt work factor used to hash the password. Increasing this number increases the amount of time it takes to hash a password. This is to keep up with Moore's law. Default 10

mongoose-pass's People

Contributors

ian2000611 avatar ksmithut avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.