papers-local
v1.0.0
Published
Promised based local username and password authentication strategy for Papers.
Downloads
15
Maintainers
Readme
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