Passport Authentication immediately after New User Registration
Here's the solution I came up with after reading about req.login:
app.post('/register', function(req, res) {
// attach POST to user schema
var user = new User({ email: req.body.email, password: req.body.password, name: req.body.name });
// save in Mongo
user.save(function(err) {
if(err) {
console.log(err);
} else {
console.log('user: ' + user.email + " saved.");
req.login(user, function(err) {
if (err) {
console.log(err);
}
return res.redirect('/dashboard');
});
}
});
});
I would like to clean it up a bit and think that the err section could be more robust, but this is a functioning solution. Note that is someone else implements this, they should be aware that it is tailored to using the passport-local strategy with email instead of username.