Passport strategy for authenticating with a usernameand password.
This module lets you authenticate using a username and password in your Node.jsapplications. By plugging into Passport, local authentication can be easily andunobtrusively integrated into any application or framework that supportsConnect-style middleware, includingExpress.
Install
$ npm install passport-local
Usage
Configure Strategy
The local authentication strategy authenticates users using a username andpassword. The strategy requires a verify
callback, which accepts thesecredentials and calls done
providing a user.
passport.use(new LocalStrategy( function(username, password, done) { User.findOne({ username: username }, function (err, user) { if (err) { return done(err); } if (!user) { return done(null, false); } if (!user.verifyPassword(password)) { return done(null, false); } return done(null, user); }); }));
Authenticate Requests
Use passport.authenticate()
, specifying the 'local'
strategy, toauthenticate requests.
For example, as route middleware in an Expressapplication:
app.post('/login', passport.authenticate('local', { failureRedirect: '/login' }), function(req, res) { res.redirect('/'); });
Examples
For complete, working examples, refer to the multiple examples included.
Tests
$ npm install$ npm test
Credits
License
Copyright (c) 2011-2014 Jared Hanson <http://jaredhanson.net/>