MM YFI
MM YFI

Reputation: 67

What does `(req,res,next)` at bottom mean?

router.post('/login', isNotLoggedIn, (req, res, next) => {
    passport.authenticate('local', (authError, user, info) => {
        if (authError) {
            console.error(authError);
            return next(authError);
        }
        if (!user) {
            req.flash('loginError', info.message);
            return res.redirect('/');
        }

        return req.login(user, (loginError) => {
            if (loginError) {
                console.error(loginError);
                return next(loginError);
            }

            return res.redirect('/');
        });
    })(req, res, next);// <-- this line
});

I'm trying to learning passport package from online lecture. But, I don't get this (req,res,next) at bottom. Can anybody help me out what does it mean?

Upvotes: 0

Views: 1480

Answers (2)

Aritra Chakraborty
Aritra Chakraborty

Reputation: 12552

According to the Passport Doc:

authenticate()'s function signature is standard Connect middleware, which makes it convenient to use as route middleware in Express applications.

So, basically the connect middleware has this type of structure:

function middleware(req, res, next){
   //do something
}

You can check what are connect type middlewares from express.js docs.

But ultimately, passport.authenticate() returns a function which is identical to the above signature.

So when you do

passport.authenticate(.....)(req, res, next)

You are doing this:

(function(req, res, next){
   //do something
})(req, res, next)

And you are basically passing express.request as req, express.response as res and next which is pointer to the next handler.

Upvotes: 0

(...) after any function declaration is the standard execution operator:

const fn = function test() {
  return 'test';
}

will result in fn being a function.

const fn = (function test() {
  return 'test';
})();

will result in fn being the string test because the declared function gets run before the assignment happens, similar to:

const tempfunction = function() {
   return 'test';
};

const fn = tempFunction();

but without that intermediary tempFunction hanging around.

In that vein, the code you're showing is functionally equivalent to this:

router.post("/login", isNotLoggedIn, (req, res, next) => {
  // declare a passport authentication handler
  const authFn = (authError, user, info) => {
    if (authError) {
      console.error(authError);
      return next(authError);
    }
    if (!user) {
      req.flash("loginError", info.message);
      return res.redirect("/");
    }

    return req.login(user, loginError => {
      if (loginError) {
        console.error(loginError);
        return next(loginError);
      }

      return res.redirect("/");
    });
  };

  // call passport to generate an express middleware function
  const passportMiddleware = passport.authenticate("local", authFn);

  // run
  passportMiddleware(req, res, next);
});

Upvotes: 1

Related Questions