gojohnnygo gojohnnygo - 1 year ago 863
Node.js Question

Node + Express + Passport: req.user Undefined

My question is similar to this one, but there was no insight into his solution.

I'm using Passport to auth using Instagram. After successful auth, users are directed to "/". At this point, the request has the user object (aka it's working). However, once I redirect, the req.user is undefined. :'(

The odd part is that passport.deserializeUser is being called with each request. It's successfully getting the user object, but somewhere along the middleware road, req.user is not being set (or being unset).

// on successful auth, goto "/"
app.get('/', function(req, res) {
// if the request has the user object, go to the user page
if (req.user) {
res.redirect("/user/" + req.user._id);


app.get('/user/:uid', function(req, res) {
console.log(req.user) // undefined

Answer Source

Have you set up session state for your app? You need something like this...

app.use(session({ secret: 'anything' }));