Git Product home page Git Product logo

objection-password's Introduction

Automatic Password Hashing for Objection.js Build Status

This plugin automatically adds automatic password hashing to your Objection.js models. This makes it super-easy to secure passwords and other sensitive data.

Under the hood, the plugin uses bcrypt for hashing.

Installation

NPM

npm i objection-password

Yarn

yarn add objection-password

Usage

Hashing your data

// import the plugin
const Password = require('objection-password')();
const Model = require('objection').Model;

// mixin the plugin
class Person extends Password(Model) {
    static get tableName() {
        return 'person';
    }
}

const person = await Person.query().insert({
    email: '[email protected]',
    password: 'q1w2e3r4'
});

console.log(person.password);
// $2a$12$sWSdI13BJ5ipPca/f8KTF.k4eFKsUtobfWdTBoQdj9g9I8JfLmZty

Verifying the data

// the password to verify
const password = 'q1w2e3r4';

// fetch the person by email
const person =
    await Person.query().first().where({ email: '[email protected]'});

// verify the password is correct
const passwordValid = await person.verifyPassword(password);

Options

There are a few options you can pass to customize the way the plugin works.

These options can be added when instantiating the plugin. For example:

// import the plugin
const Password = require('objection-password')({
    passwordField: 'hash'
});

allowEmptyPassword (defaults to false)

Allows an empty password to be set.

passwordField (defaults to password)

Allows you to override the name of the field to be hashed.

rounds (defaults to 12)

The number of number of bcrypt rounds to use when hashing the data.

objection-password's People

Contributors

jamesdixon avatar timhuff avatar andrewsuzuki avatar jordaaash avatar dlom avatar venables avatar

Watchers

James Cloos 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.