Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passport.js and process.nextTick in strategy

I'm facing something new in nodeJS: process.nextTick

In some strategies code examples for passport.js, we can see

passport.use(new LocalStrategy(
  function (username, password, done) {

    // asynchronous verification, for effect...
    process.nextTick(function () {

      findByUsername(username, function (err, user) {
        // ...
        bcrypt.compare(password, user.password, function (err, res) {
          // ...
        });
      })
    });
  }
));

But in the official documentation, it is not used. (http://passportjs.org/guide/username-password/)

What I understand is that process.nextTick should be used to defer synchronous stack to not block an event. But in this strategy code, there is no event.

What the benefit of doing that here ?

like image 869
m4tm4t Avatar asked Dec 23 '13 12:12

m4tm4t


People also ask

What is passport js strategy?

Passport is authentication middleware for Node. js. Extremely flexible and modular, Passport can be unobtrusively dropped in to any Express-based web application. A comprehensive set of strategies support authentication using a username and password, Facebook, Twitter, and more.

What is process nextTick?

nextTick ( ) is not the part of any phase of event loop. Instead, nextTickQueue will process all the callbacks after completing current iteration and before starting the next iteration of event loop. process. nextTick ( ) schedule a callback function to be executed in next iteration of event loop.

What is passport authenticate ()?

In this route, passport. authenticate() is middleware which will authenticate the request. By default, when authentication succeeds, the req. user property is set to the authenticated user, a login session is established, and the next function in the stack is called.

Is Passport js a middleware?

Passport is a popular, modular authentication middleware for Node. js applications. With it, authentication can be easily integrated into any Node- and Express-based app.


Video Answer


1 Answers

It's only there in the example to show that async authentication is possible. In most cases, you'd be querying a database, so it'd be async in nature. However, the example just has a hard-coded set of users, so the nextTick call is there for effect, to simulate an async function.

like image 102
Jared Hanson Avatar answered Oct 04 '22 15:10

Jared Hanson