0

I am using passport-google-oauth20, mongoose, mlab for user authentication. Once I get the callback from google auth I am getting the following error in done mehhod:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): Error: passport.initialize() middleware not in use

I have attached the screenshot of my code base. Thanks in advance!

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);})
            }
        })
   }
));

4

1 回答 1

1

请检查此passport-google-oauth2 您的错误看起来像是您错过了 Initialize passport :

app.use( passport.initialize());
app.use( passport.session());
于 2018-01-25T13:21:54.123 回答