Google oauth20的护照,未使用的中间件,passport.initialize()

时间:2018-01-25 11:26:38

标签: node.js express passport.js mlab passport-google-oauth

我使用passport-google-oauth20,mongoose,mlab进行用户身份验证。 一旦我从google auth获得回调,我在完成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);})
            }
        })
   }
));

1 个答案:

答案 0 :(得分:1)

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

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