eversor
eversor

Reputation: 3073

Express 4 & Passport Unable to Create Session

I have been able this far to log users in using Facebook. However, once I recover the information from Facebook I lose it.

I do not know if I should be the one who creates the session or I am missing something out.

This is what I have done: Configuration

var passport = require('passport');
passport.use(
  new FacebookStrategy(
    {
      clientID: FACEBOOK_APP_ID,
      clientSecret: FACEBOOK_APP_SECRET,
      callbackURL: 'http://localhost:8080/auth/facebook/callback'
    },
    function(accessToken, refreshToken, profile, done) {
      process.nextTick(function() {
        //Assuming user exists
        console.log("CALLBACK passport.use", profile);
        return done(null, profile); //SHOWS THE USER
      });
    }
  )
);

Serialization and deserialization, both console.log show correctly the user

passport.serializeUser(function(user, done) {
  console.log("SERIALIZE USER", user)
  done(null, user); //SHOWS USER
});

passport.deserializeUser(function(obj, done) {
  console.log("DESERIALIZE USER", obj)
  done(null, obj); //SHOWS USER
});

Inside the cluster (1 of 4), it makes correctly the redirection to /success. However, the console.log of the successcontroller shows undefined. I have tried without clusters, and the issue remains the same...

else if (!cluster.isMaster) {
//...
    app.use(passport.initialize());
    app.use(passport.session());

    app.get('/auth/facebook', passport.authenticate('facebook'));
    app.get('/auth/facebook/callback',
       passport.authenticate('facebook', {
           successRedirect : '/success',
           failureRedirect : '/'
       }));

    app.get('/success', function(req, res, next) {
      console.log("\n\n\nSESSION", req.user); //SHOWS req.user as UNDEFINED ;(
      res.send('Successfully logged in.');
    });

Upvotes: 0

Views: 232

Answers (1)

robertklep
robertklep

Reputation: 203554

Passport uses the session middleware that Express is configured to use, which usually is express-session.

By default, this middleware uses an in-memory store to keep the session data in, but aside from this not being persistent across restarts of your app and having known memory leaks, the session data is not shared across your various worker processes (the in-memory storage area is per-process).

Some kind of shared session store that can be used across all worker processes should be used instead. There are various options, of which I think the Redis and MongoDB stores are most popular.

Upvotes: 1

Related Questions