I have two files in routes folder - index.js and api.js.
index.js only sends the main template page with linked styles, images and scripts.
api.js manages all the API calls.
I am implementing authentication with PassportJS. Authentication itself works fine. The issue is that for some reason I get undefined when trying to access req.user from api.js endpoints, but it is fine and filled with proper data when accessed from index.js.
For example, I need to send as JSON all user's posts when requested from api endpoint so I want to get req.user.id or something like this to send a request to database on server side.
What could I miss while configuring Passport?
You must include the passport's session middleware before all your routes so it can set req.user.
app.use(express.session({ secret: 'secret' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(myRoutes);
Make sure you're getting ._id instead of .id, this is the default ID creation in MongoDB.