javascript syntax while doing passport nodejs Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of javascript syntax while doing passport nodejs without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have a question on javascript syntax. Actually I came up with the coding while I was self-teaching MEAN stack tutorial (https://thinkster.io/mean-stack-tutorial#adding-authentication-via-passport). There is a very weired coding below.

})(req, res, next);

(req, res, next) seems arguments but no function utilize that arguments. Maybe I am not smart enough at this point so that I cannot see it. Is there any guy who can help me on this? Thanks.

router.post('/login', function(req, res, next){
 if(!req.body.username || !req.body.password){
  return res.status(400).json({message: 'Please fill out all fields'});
 }

 passport.authenticate('local', function(err, user, info){
  if(err){ return next(err); }

  if(user){
   return res.json({token: user.generateJWT()});
  } else {
   return res.status(401).json(info);
  }
 })(req, res, next);
});

Answer

To understand what’s happening, you should know what “middleware” is in Express. It’s a function that you can pass to Express that gets passed a request object, a response object, and a next function:

function middleware(req, res, next) {
...
}

With middleware, you can “tap” into the path that HTTP requests will follow through an Express application, and perform certain actions.

You might have already noticed that the middleware function signature looks a lot like your example code:

router.post('/login', function(req, res, next) { ... });

This is a route handler that gets called for POST requests to /login. Route handlers are similar to middleware, in that they get called with the same arguments and also perform certain actions (usually, next isn’t used in route handlers, but it will still get passed as an argument).

You can “stack” middleware, too:

router.post('/login',
    function (req, res, next) { ... }, // first middleware
    function (req, res, next) { ... }, // second middleware
    ...
);

This is where next comes into play: if the first middleware isn’t interested in the request, it can call next (which is a function) and the request will be passed to the second middleware (and if that middleware isn’t interested in it, it can call next too, passing the request along all middleware in the app, until a middleware handles the request or it falls through, generating a 404 error, because there no middleware was found that could handle the request).

passport.authenticate() also returns a middleware function. It’s usually used like this:

router.post('/login',
  passport.authenticate(...),
  function (req, res, next) { ... }
);

Which means that if you look at the stacking example, passport.authenticate() should return a function that accepts the three arguments req, res and next (and in fact, it does).

That means that the code above can be rewritten to this:

router.post('/login', function(req, res, next) {
  passport.authenticate(...)(req, res, next);
});

Which matches the code in your question. Why you would want to call passport.authenticate() like that is a relatively advanced Passport topic.

EDIT: this is what passport.authentication, in very broad terms, looks like:

// a function that mimics what `passport.authenticate` does:
function myAuthenticate() {
  return function (req, res, next) {
    ...some stuff...
    next();
  };
}

It’s a function that returns a function. You can use it like this:

router.post('/login',
  myAuthenticate(),
  function (req, res, next) {
    ...
  }
);

Which is (almost) the same as this:

router.post('/login',
  function(req, res, next) { // <-- this is the function that got returned!
    ...some stuff...
    next();
  },
  function(req, res, next) {
    ...
  }
);
We are here to answer your question about javascript syntax while doing passport nodejs - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji