I am having difficulty conceptualizing what goes on here. Is my commentary valid or is it acting in a different way?
app.post('/auth/signin', function(req, res, next) {
passport.authenticate('local', null,function(error, user, info) {
if (error) { return next(error) }
if (user === false) { return res.json({ message : 'Invalid user'}); }
req.logIn(user, function(error) {
if (error) { return next(error); }
return res.json({ userInfo : user })
});
})(req, res, next) // there is a returned inner function that requires these 3 inputs.
// I guess authenticate() returns a route handler function
});
In this example, note that
authenticate()
is called from within the route handler, rather than being used as route middleware. This gives the callback access to thereq
andres
objects through closure.
I remember a closure as being an inner function for holding state, so I’m thinking what’s happening is: the outer function passes its arguments to the closure to be processed in the callback function. Is that right?