Node + Express + Passport: req.user Undefined
My issue was not specifying to send cookies when using fetch on the client-side. It worked after including the credentials: 'include' field in the request.
fetch('/api/foo', {credentials: 'include'})
I am super new to Node but it was a middleware issue for me. Added bodyParser and rearranged to fix.
Broken code:
app.use(express.cookieParser('secret'));
app.use(express.cookieSession());
app.use(express.session({ secret: 'anything' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
Working Code:
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.session({ secret: 'anything' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
Hope this helps
Have you set up session state for your app? If you haven't, you need something like this...
app.use(session({ secret: 'anything' }));
app.use(passport.initialize());
app.use(passport.session());