passport.js and process.nextTick in strategy

后端 未结 2 2008
我在风中等你
我在风中等你 2020-12-31 12:07

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

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

passport.use(new Loca         


        
相关标签:
2条回答
  • 2020-12-31 12:20

    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 讨论(0)
  • 2020-12-31 12:27

    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.

    0 讨论(0)
提交回复
热议问题