I\'m working on a webapp that requires atuhentication process and session management with express. I\'ve done with the backend sessions stuff. Now i want to show on the UI t
In this case you have a few options (only use one of these!):
req
objectIn your exports.privateContent function, once a user is found in the database, you can simply add that data to the res.locals:
User.findOne( { 'username': username }, function ( err, obj ) {
if ( true ) {
// this variable will be available directly by the view
res.locals.user = obj;
// this will be added to the request object
req.user = obj;
next();
} else {
res.redirect('/');
}
});
Then in your exports.my_tasks route, res.locals.user
will be whatever obj was in the middleware. You can then simply access that in the view as the variable user
.
So, all together, you can access the data in your route in these ways:
exports.my_tasks = function ( req, res ) {
res.render('tasks/tasks', {
userFromReq: req.user, // this exists because you added in the middleware
userFromSession: req.session.user, // this was already in the session, so you can access
userFromRes: [DO NOT NEED TO DO THIS] // because res.locals are sent straight to the view (Jade).
});
};