Authentication for Objection.js
This package includes plugins useful for authentication for websites:
- Authenticatable - Generates hashed passwords for a user model. Uses
bcrypt
under the hood. - Recoverable - Generates password reset tokens.
Installation
npm install objection-auth
Usage
Authenticatable
// Import the plugin.const Authenticatable = ;const Model = ; // Mixin the plugin.const AuthenticatableModel = Model; // Create your model. // ...code
Verifying a password
In your login controller logic:
const user = await User; if !userverifyPassword // throw an error
Options
passwordField
(required)
The field to that the hashed password will be stored on. (required, defaults to 'password')
saltRounds
(defaults to slug
)
The number of salt rounds as passed to bcrypt
.
Recoverable
// Import the plugin.const Recoverable = ;const Model = ; // Mixin the plugin.const RecoverableModel = Model; // Create your model. // ...code
Generating a reset token
In your reset password controller logic:
const user = await User; await user;console;//
Options
tokenField
(defaults to resetPasswordToken
)
The field that the reset token is stored on.
tokenExpField
(defaults to resetPasswordExp
)
The field that the expiration date is stored on.
expiresIn
(defaults to 60
minutes)
The expiration time of the token, in minutes.
Chaining Plugins
These plugins can be used together by composing the plugins together:
const Authenticatable Recoverable = ;const compose Model = ; const mixins = ; Model // ...