I am using passport to login my users. When a user logs into my app, the username will be displayed in the header.
This is my header.jade:
div#header_content
input(type='text',name='search',id='globle_search')
span#user_status
if(req.isAuthenticated())
a(class='user_menu_btn',href='home', target='_blank' ) req.user.username
a(class='user_menu_btn',href='logout') Logout
else
a(id='login_btn',href='login',class='user_status_btn') login
If I run the app, I get an error that says:
ReferenceError: F:\shopping\views\includes\header.jade:4
req is not defined
This is my index route:
app.get('/',index.index);
exports.index = function(req, res){
res.render('index', { title: 'Express' });
};
How do I maintain the login status in the header?
To access
reqwithin the view, it has to be member of thelocalsobject passed to the view engine — which it won’t be by default.Though, since
userandisAuthenticatedare the what you need fromreq, you can just attach them directly:See
res.localsandapp.localsfor more info.