0

I'm trying to setup google auth with PassportJS, but my deserializeUser function is never called.

This is my passport.js config:

...
passport.serializeUser(function(user, done) {

        done(null, user.id);

    });

    passport.deserializeUser(function(sessionUser, done) {

      User.findById(sessionUser, function(err, user) {
             done(err, user);
         });
  });

passport.use('google', new GoogleStrategy({
        clientID        : process.env.GOOGLE_CLIENTID,
        clientSecret    : process.env.GOOGLE_CLIENTSECRET,
        callbackURL     : process.env.GOOGLE_CALLBACKURL,
        passReqToCallback: true,
    },
        function(req, token, refreshToken, profile, done) {


            process.nextTick(function() {

                // console.log(profile);

                var values = { 
                    where: { google_id: profile.id }, 
                    defaults: {google_id: profile.id, name: profile.displayName} 
                };

                User.findOrCreate(values)
                .spread(function(user, created) {
                    return done(null,user);
                });
            });
        }
    ));

And these are my routes:

app.get('/auth/google', 
    passport.authenticate('google', { scope : ['profile', 'email'] }));


// the callback after google has authenticated the user
app.get('/auth/google/callback',
        passport.authenticate('google', {
                successRedirect : '/portfolio/crypto',
                failureRedirect : '/'
        }));

And since deserializeUser is never called, req.user is never defined. How can I fix this?

Filipe Ferminiano
  • 8,373
  • 25
  • 104
  • 174
  • do you mean callback method of deserializeUser never gets executed ? If not, try to console.log(sessionUser) and see what is the value, it should be a string. – Raj Kumar Oct 14 '18 at 19:48
  • @RajKumar no, passport.deserializeUser is never executed. If I put a console.log at deserializeUser I never see this console.log been printed in my console – Filipe Ferminiano Oct 14 '18 at 20:12
  • This thread my be useful https://stackoverflow.com/questions/11277779/passportjs-deserializeuser-never-called – Raj Kumar Oct 14 '18 at 20:42
  • @RajKumar already checked this thread. Nothing helped – Filipe Ferminiano Oct 14 '18 at 21:07
  • Bad luck mate. In that case what would I have done is grab a working example from someone's repo and check if worked for me, check if other module/package such as express-session is doing something in-between, using latest/stable packages etc. Never stop trying and thinking of the solution. – Raj Kumar Oct 14 '18 at 21:33

0 Answers0