passport-request
v1.0.0
Published
Request authentication strategy for Passport.
Downloads
5
Readme
passport-request
Passport strategy for authenticating against
Express' request
object.
Install
$ npm install passport-request
Usage
Configure Strategy
The strategy requires a verify
callback, which gets passed the request object and a done
callback that should be called with the results of the authentication.
For example, to allow authentication using POST /login/:username
and a password
parameter in the request body:
var RequestStrategy = require('passport-request').Strategy;
passport.use(new RequestStrategy(function(req, done) {
var username = req.params.username;
var password = req.body.password;
User.findOne({ username : username }, function (err, user) {
if (err) {
return done(err);
} else if (! user) {
return done(null, false);
} else if (! user.verifyPassword(password)) {
return done(null, false);
} else {
return done(null, user);
}
});
));
Authenticate Requests
Use passport.authenticate()
, specifying the 'request'
strategy, to
authenticate requests.
For example, as route middleware in an Express application:
app.post('/login/:username',
passport.authenticate('request', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
}
);