Passport Middleware For Node.js
I am using Passport authentication middleware for Node.js. I would like to use it from within my own objects (rather than inline in a route definition). The reason for this that th
Solution 1:
Here is an example I am using :
// Bootstrap controllers
var controllers_path = __dirname + '/controllers';
fs.readdirSync(controllers_path).forEach(function(file) {
if (~file.indexOf('.js')) {
app.controllers[file.slice(0, - 3)] = require(controllers_path + '/' + file);
}
});
My router:
app.server.post('/login', app.controllers.users.doLogin);
And my user controller :
exports.doLogin = function(req, res, next) {
app.passport.authenticate('local', function(err, user, info) {
if (err) {
return next(err);
}
if (!user) {
req.flash('error', 'Invalid username or password');
return res.redirect('/login' + (req.body.target ? '?target=' + req.body.target : ''));
}
req.logIn(user, function(err) {
if (err) {
return next(err);
}
return res.redirect(req.body.target || '/');
});
})(req, res, next);
});
Post a Comment for "Passport Middleware For Node.js"