最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - passport.js and process.nextTick in strategy - Stack Overflow

programmeradmin0浏览0评论

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) {
        // ...
        bcryptpare(password, user.password, function (err, res) {
          // ...
        });
      })
    });
  }
));

But in the official documentation, it is not used. (/)

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 ?

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 ?

Share Improve this question edited Jan 6, 2015 at 19:12 Martijn Pieters 1.1m320 gold badges4.2k silver badges3.4k bronze badges asked Dec 23, 2013 at 12:14 m4tm4tm4tm4t 2,3811 gold badge21 silver badges37 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 24

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.

100% ES6 working so you can delete the nextTick
I use babel and webpack at server side to so:

import passport from 'passport';

const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;

const manipulateUser = async (User, profile, done, token) => {
  try {
    const user = await User.findOne({ googleId: profile.id });
    if (user) {
      user.accessToken = token;
      await user.save();
      return done(null, user);
    }
    const newUser = new User();
    newUser.googleId = profile.id;
    newUser.name = profile.displayName;
    newUser.avatar = profile.photos[0].value;
    newUser.accessToken = token;
    profile.emails.forEach((email) => { newUser.emails.push(email.value); });
    await newUser.save();
    return done(null, newUser);
  } catch (err) {
    console.log('err at manipulateUser passport', err);
    return done(err);
  }
};

const strategy = (User, config) => new GoogleStrategy({
  clientID: config.googleAuth.clientID,
  clientSecret: config.googleAuth.clientSecret,
  callbackURL: config.googleAuth.callbackURL,
}, async (token, refreshToken, profile, done) => manipulateUser(User, profile, done, token));

export const setup = (User, config) => {
  passport.use(strategy(User, config));
};
发布评论

评论列表(0)

  1. 暂无评论