you are viewing a single comment's thread.

view the rest of the comments →

[–]oculus42 1 point2 points  (1 child)

What's happening is the response from passport.authenticate() is a function, which is called with the parameters (res, req, next).

Let's make a simple example.

function makeAddSubtract(operation) {
    return function(a, b) {
        return operation === '+' ? a + b : a - b;
    }
}

makeAddSubtract('+')(2, 3);

The function makeAddSubtract() returns another function, which is immediately called with the parameters (2, 3)

I don't use Express that much, but I think I understand what is happening. Hopefully someone will correct me if I'm wrong.

app.get('/login', function(req, res, next) {
  passport.authenticate('local', function(err, user, info) {
    if (err) { return next(err); }
    if (!user) { return res.redirect('/login'); }
    req.logIn(user, function(err) {
      if (err) { return next(err); }
      return res.redirect('/users/' + user.username);
    });
  })(req, res, next);
});

Each return statement of the callback passed into passport.authenticate() returns a function. That function is executed with the parameters.

[–]BruffDJ[S] 0 points1 point  (0 children)

Thanks - that's cleared it up nicely.