Automatic password hashing for your bookshelf models
npm i --save bookshelf-bcrypt,
all you need to do is add it as a bookshelf plugin and enable it on your models.
let knex = developmentlet bookshelf = knex// Add the pluginbookshelf// Enable it on your modelslet User = bookshelfModel// By default, an error will be thrown if a null/undefined password is detected. Use the following to allow null/undefined passwordslet User = bookshelfModel
Nothing fancy here, just keep using bookshelf as usual.
// Wow such h4x0r, much passwordlet user = Userconsole // $2a$12$K2CtDP7zSGOKgjXjxD9SYey9mSZ9Udio9C95K6wCKZewSP9oBWyPO
This plugin will also hash the password again if it detects that the field changed, so you're good to do this:
let user = User// Update the useruseruser // Password automatically hashed with the new value// You can also avoid hashing by using an optionsuser
bookshelf-bcrypt uses 12 salt rounds by default. By default we don't try and detect
a rehash because a user may use a password that looks like a bcrypt hash. If you
add a detectBcrypt function value and it returns a truthy value, an error will be thrown.
You can also override the onRehash function in settings.
git clone firstname.lastname@example.org:estate/bookshelf-bcrypt.gitcd bookshelf-bcrypt && npm install && npm test