NodeJS, getting username of logged in user within route NodeJS, getting username of logged in user within route mongoose mongoose

NodeJS, getting username of logged in user within route


As far as I understand you are trying to pass your username to the preferably every file including your router file. What I do for this to use middleware in app.js to pass every page. Or you can simply implement passport implementation in the other page as well which could be useless i guess.

app.use(function(req,res,next){  res.locals.currentUser=req.user  next()}

Then, you can use use your currentUser in every page when you try to render.


I encountered the same issue, probably after following the same tutorials...I found that the function you need in the app.js is:

app.get('*', function(req, res,next){    res.locals.user = req.user || null;    next();})

it should be in the app.js already. Now, in all of the other page you should be able to use req.user.username