Jordy
Jordy

Reputation: 17

Google authentication: failed to serialize user into session

I'm trying to get some basic Google authentication going with PassportJS. The amount of information on the web seems rather limited and I keep hitting issues so thought I would try here.

I am using the code from https://github.com/mattgaidica/twitter-mongo with a few modifications to use it for Google OAuth (it doesn't use the Twitter keys, it uses passport-google, passport.authenticate('google', ...)).

I keep ending up with this error: 'Error: failed to serialize user into session'

passport.serializeUser(function(user, done) {
  console.log(user); //does not have the fields we created earlier, user.uid does not exist.
  done(null, user.uid);
});

My Passport Strategy:

passport.use(new GoogleStrategy({
    returnURL: "http://localhost:3000/auth/google/callback"
  },
  function(identifier, profile, done) {
    User.findOne({uid: profile.id}, function(err, user) {
      if(user) {
        done(null, user);
      } else {
        var user = new User();
        user.provider = "google";
        user.uid = identifier;
        user.name = profile.displayName;
        user.save(function(err) {
          if(err) { throw err; }
          done(null, user);
        });
      }
    })
  }
));

The fields of this other user are not the same as the one originally created, what happened to the other user?

I'm a bit lost as to what is going on here, if anyone could let me know what I'm doing wrong I would be very grateful.

Upvotes: 0

Views: 1482

Answers (1)

robertklep
robertklep

Reputation: 203359

In your query, you use profile.id:

User.findOne({uid: profile.id}, function(err, user) { ... });

But that should be identifier.

Also, you're using the OpenID-version of the Passport Google plug-in, which is rather old (and doesn't even work properly on Node 0.10). I'd suggest you use passport-google-oauth instead.

Upvotes: 1

Related Questions