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 Argon2 for hashing.
yarn add objection-password-argon2
import Password from 'objection-password-argon2'
import Model from 'objection'
class Person extends Password()(Model) {
// ...
}
const person = await Person.query().insert({
email: '[email protected]',
password: 'q1w2e3r4'
});
console.log(person.password);
// $argon2i$v=19$m=4096,t=3,p=1$yqdvmjCHT1o+03hbpFg7HQ$Vg3+D9kW9+Nm0+ukCzKNWLb0h8iPQdTkD/HYHrxInhA
// 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);
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 Password from 'objection-password-argon2'
class Person extends Password({ passwordField: 'hash' })(Model) {
// ...
}
Allows an empty password to be set.
Allows you to override the name of the field to be hashed.