Code Monkey home page Code Monkey logo

objection-auth's Introduction

Build Status Coverage Status

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 } = require('objection-auth');
const { Model } = require('objection');

// Mixin the plugin.
const AuthenticatableModel = Authenticatable({
  passwordField: 'password',
  saltRounds: 12,
})(Model);

// Create your model.
class User extends AuthenticatableModel {
  // ...code
}

Verifying a password

In your login controller logic:

const user = await User.query().where('id', 1);

if (!user.verifyPassword) {
  // 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 } = require('objection-auth');
const { Model } = require('objection');

// Mixin the plugin.
const RecoverableModel = Recoverable({
  tokenField: 'resetPasswordToken',
  tokenExpField: 'resetPasswordExp',
  expiresIn: 60
})(Model);

// Create your model.
class User extends RecoverableModel {
  // ...code
}

Generating a reset token

In your reset password controller logic:

const user = await User.query().where('id', 1);

await user.generateResetToken();
console.log(user.resetPasswordToken);
//

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 } = require('objection-auth');
const { compose, Model } = require('objection');

const mixins = compose(
  Authenticatable({ saltRounds: 10, passwordField: 'pass' }),
  Recoverable({
    tokenField: 'resetPasswordToken',
    tokenExpField: 'resetPasswordExp',
    expiresIn: 60
  })
);

class User extends mixins(Model) {
  // ...
}

objection-auth's People

Contributors

calvinl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  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.