Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between 'done' and 'next' in node.js callbacks

in the passport [configure authentication] documentation, it has a rather scary-looking function that uses the mysterious function "done.'

passport.use(new LocalStrategy(
  function(username, password, done) {
    User.findOne({ username: username }, function (err, user) {
      if (err) { return done(err); }
      if (!user) {
        return done(null, false, { message: 'Incorrect username.' });
      }
      if (!user.validPassword(password)) {
        return done(null, false, { message: 'Incorrect password.' });
      }
      return done(null, user);
    });
   }
));

Now, in the express documentation there are quite a few methods that pass something called next.

app.use(function(err, req, res, next){
  console.error(err.stack);
  res.status(500).send('Something broke!');
});

Is this the difference between the two frameworks, express and passport? Or are they doing two separate things?

like image 967
Tara Roys Avatar asked Oct 02 '14 16:10

Tara Roys


People also ask

What does next () do in Nodejs?

next() : It will run or execute the code after all the middleware function is finished. return next() : By using return next it will jump out the callback immediately and the code below return next() will be unreachable.

What is next callback?

callback: It is the callback function that contains the request object, response object, and next() function to call the next middleware function if the response of the current middleware is not terminated. In the second parameter, we can also pass the function name of the middleware.

What does next () do in js?

The next() method returns an object with two properties done and value . You can also provide a parameter to the next method to send a value to the generator.

What does next () do Express?

The next function is a function in the Express router which, when invoked, executes the middleware succeeding the current middleware. Middleware functions can perform the following tasks: Execute any code.


1 Answers

Is this the difference between the two frameworks, express and passport?

No they are different in the purpose for which they are used for. Express is used as a application framework on node.js where as passport just handles the authentication part of a web application.

about next()

next() is part of connect which inturn is an express dependency. The purpose of calling next() is to trigger the next middle ware in express stack.

To understand the next() concept in an easier way, you could look at a sample app built on express here.

as you can see in the line pointed the application uses a route level middleware to check whether the user is logged in or not.

app.get('/account', ensureAuthenticated, function(req, res){

Here ensureAuthenticated is the middleware which is defined at bottom like

function ensureAuthenticated(req, res, next) {
  if (req.isAuthenticated()) { return next(); }
  res.redirect('/login')
}

as you can see if the user is authenticated the function invokes next() and passes control to the next layer in route handler written above, else it redirects to another route even without invoking the next()

about done()

done() on the other hand is used to trigger the return url handlers that we write for passport authentication. To understand more on how done works you could look at the code samples at passport here and check the section titled Custom Callback

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);
});

Here the second parameter to passport.authenticate is the definition of done() that you are going to call from the passport strategy.

note

Here going through the sample codes in the two links I provided above have helped alot in understanding its behavior than the docs. I would suggest you to do the same.

like image 121
Mithun Satheesh Avatar answered Oct 11 '22 13:10

Mithun Satheesh