After the initial successful login from Facebook and the callback redirection of the username using passport-facebook 1.0.3 and expression 4.6.1, req.session.passport and req.user contain the value set during the serialization call (which I get from stragegy), but on subsequent visits to different routes on the site, req.session.passport and req.user is empty, and req.isAuthenticated () returns false, so after the initial successful login to the FB system, the secureAuthentication method on all other routes fails. I donβt use cluster configuration, so I think that there is enough memory to handle this, the express configuration looks fine (I mean the order), here is my express configuration
configExpressApp.set('views', './views'); configExpressApp.set('view engine', 'jade'); configExpressApp.use(morgan('dev')); configExpressApp.use(cookieParser()); configExpressApp.use(bodyParser.urlencoded({ extended: true, })); configExpressApp.use(bodyParser.json()); configExpressApp.use(expressSession({ secret:'MyExpressSecret', saveUninitialized: true, resave: true })); configExpressApp.use(passport.initialize()); configExpressApp.use(passport.session()); configExpressApp.use(methodOverride()); configExpressApp.use(express.static('./public'));
Here is the req.session object upon initial successful login and redirection. Req.user contains the same data as req.session.passport.user
{ cookie: { path: '/', _expires: null, originalMaxAge: null, httpOnly: true }, passport: { user: { _id: 53ce23e3121421f229a438f8, info: false, loginType: 'fb', fbId: 'MyId', name: 'Karthic Rao', email: 'kartronics85@yahoo.com' } } }
this is information that was previously associated with the done () callback inside the strategy, as well as inside the serialization call. After a successful login and callback, I use res.redirect to redirect the user to another route, but the requests coming from this route contain sessionID (so I don't think this is a problem with the session store), but the req.user field does not exist ( maybe because the passport .initialize () and passport.session () middlewares do not find a request for authentication), and the req.session.passport field is empty, here are the details from the console.log req object.
sessionID: 'I9R1c3PIYgDW5OpWbNT7qb02Hn4lOeAB', session: { cookie: { path: '/', _expires: null, originalMaxAge: null, httpOnly: true }, passport: {} },
Here is my deserialization method
passport.deserializeUser(function(user, done) { console.log('deserialize loginType facebook'); db.collection("users").findOne({ fbId: user.id }, function(err, docs) { console.log(docs); done(err, docs); }); });
Here is my serialization method
passport.serializeUser(function (user, done) { console.log(user); done(null, user); });
This creates a big obstacle to my development, how can I figure it out?