You need to open a session for templates. The way I do this is that I put my user variable in local:
Note: req.user is defined by a JS passport, you can see their example from here (below)
Note # 2: More about local here
app.get('*', function(req, res, next) { // put user into res.locals for easy access from templates res.locals.user = req.user || null; next(); });
and then in the template you can do this:
- if(_user) { div.menuForLoggedInUser - } else { div.menuForLoggedOutUser - }
If you don't want to expose the entire user object to templates, feel free to just put "loggedIn" in locals and check that ... Something like this:
app.get('*', function(req, res, next) { // just use boolean for loggedIn res.locals.loggedIn = (req.user) ? true : false; next(); });
Edit: Thanks robertklep for specifying res.locals vs app.locals
source share