I am using the standard example from here which contains:
app.get('/login/facebook',
passport.authenticate('facebook'));
app.get('/login/facebook/return',
passport.authenticate('facebook', { failureRedirect: '/login' }),
function(req, res) { //req.user is populated
res.redirect('/');
});
app.get('/', function (req, res) {
res.render('index.jade', { user: req.user }); //req.user is undefined
});
The login appears to work fine. In /login/facebook/return
I could see req.user
and its details. But after getting redirected to /
, req.user
becomes undefined
.
Is there anything else I must add to make it work?
According to Passportjs Documentations, passport.initialize()
and passport.session()
(for persistent logins) middlewares should be added to the app
after express-session
.
From the docs:
Note that enabling session support is entirely optional, though it is recommended for most applications. If enabled, be sure to use
express.session()
beforepassport.session()
to ensure that the login session is restored in the correct order.
app.configure(function() {
app.use(express.static('public'));
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.session({ secret: 'keyboard cat' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
});