Google oauth20 的通行证,未使用中间件,passport.initialize()

Passport for Google oauth20, middleware not in use, passport.initialize()

我正在使用 passport-google-oauth20、mongoose、mlab 进行用户身份验证。 一旦我从 google auth 获得回调,我在 done mehhod 中收到以下错误:

UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝 ID:3):错误:passport.initialize() 中间件未在使用中

我附上了我的代码库的截图。 提前致谢!

const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const mongoose = require('mongoose');
const Keys = require('../config/dev');
const User = mongoose.model('users');

passport.serializeUser((user, done) => {
  done(null, user.id);
});
passport.deserializeUser((id, done) => {
  User.findById(id).then(user => {
    done(null, user);
  });
});
passport.use(new GoogleStrategy({
    clientID: Keys.GOOGLE_CLIENT_ID,
    clientSecret: Keys.GOOGLE_CLIENT_SECRETKEY,
    callbackURL: "/auth/google/callback"
  },(accessToken, refreshToken, profile, done) => {
        User.findOne({googleID: profile.id}).then((existingUser) => {
            if(existingUser){
                console.log('existing');
                //This above log is printing fine and then here I am getting error
                done(null, existingUser);
            } else{
                console.log('new');
                new User({googleID: profile.id}).save()
                .then((user) => {done(null, user);})
            }
        })
   }
));

请检查这个passport-google-oauth2 你的错误看起来像你错过了初始化护照:

app.use( passport.initialize());
app.use( passport.session());