Giter Club home page Giter Club logo

papers-local's Introduction

Build Status Code Climate Test Coverage Issue Count

papers-local

papers strategy for authenticating with a username and password.

This module lets you authenticate using a username and password in your Node.js applications. By plugging into Papers, local authentication can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express. This strategy also works with both KOA and KOA2

This module was ported from passport-local whose inspiration we greatly appreciate

Install

$ npm install papers-local

Usage

Configure Strategy

The local authentication strategy authenticates users using a username and password. The strategy requires a validate function, which accepts these credentials as well as passing in the request (ctx in KOA) and returns either a user, a falsy or throws an error.

var localStrategy(
  function(username, password, req) {
    return User.findOne({ username: username }, function (err, user) {
      if (err) { return throw(err); }
      if (!user) { return false; }
      if (!user.verifyPassword(password)) { return (false); }
      return user;
    });
  }
));
Available Options

This strategy takes an optional options hash after the validate function, e.g. localStrategy(validate, {/* options */}).

The available options are:

  • usernameField - Optional, defaults to 'username'
  • passwordField - Optional, defaults to 'password'

Both fields define the name of the properties in the POST body that are sent to the server.

Parameters

By default, LocalStrategy expects to find credentials in parameters named username and password. If your site prefers to name these fields differently, options are available to change the defaults.

localStrategy(function(username, password, req) {
    // ...
  }, {
    usernameField: 'email',
    passwordField: 'passwd'
  }
);

Authenticate Requests

Use papers().registerMiddleware(config) specifying your localStrategy in the strategies config.

var papersConfig = {
  strategies: [ localStrategy ]
}
app.use(papers().registerMiddleware(config));

or for a specific endpoint

app.post('/profile', papers().registerMiddleware(config),
    function(req, res) {
        res.send(req.user.profile);
    }
);

Tests

$ npm install
$ npm test

Credits

  • Thank you to Jared Hanson for passport, the inspiration for this library

License

The MIT License

papers-local's People

Contributors

reharik avatar

Watchers

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