I can’t understand why the method next(null, user)
is called at the end of the method below, if someone could help explain this to me, I would greatly appreciate it!
app.route('/register')
.post((req, res, next) => {
db.collection('users').findOne({username: req.body.username}, function(err, user) {
if(err) {
next(err);
}
else if(user){
res.redirect('/');
}
else{
db.collection('users').insertOne(
{username: req.body.username,
password: req.body.password},
(err, doc) => {
if(err) {
res.redirect('/');
}
else{
next(null, user); // Why would this be called, instead of just next()? Why the parameters?
}
}
)
}
})},
passport.authenticate('local', {failureRedirect: '/'}),
(req, res) => {
res.redirect('/profile');
}
);