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);})
}
})
}
));
Please check this passport-google-oauth2 Your error look likes you miss to Initialize passport :
app.use( passport.initialize());
app.use( passport.session());