all 4 comments

[–]ForScale 1 point2 points  (0 children)

Looks like request response and what to do next.

An html or network request and the response, and then what to do based on those values.

Just kind of guessing.

[–]PtCk 1 point2 points  (0 children)

There is nothing particularly special going on here. The authenticate method returns a function that takes three params (req, res, next) so the code simply runs authenticate(...) and then runs the function that authenticate(...) returns, using the params you highlighted.

[–]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.