0
 app.use(function (req, res, next) {
 res.locals.login = req.isAuthenticated();
 app.locals.userinfo = req.user;    
 next();
});

i am using global variables to store user info as i need to display them in my main template. is it good practice to store user info in variables or is there a workaround? are there any performance issue in using variables?

i tried creating helpers but that doesnt seems to work.

passing value while rendering is also not possible as user info has to be displaye on nav bar that is part of my main template.

2
  • 1
    Why don't you put the user object inside res.locals? The middleware you have right now looks fine but I would suggest to have the user object set to res.locals Commented Jul 31, 2019 at 10:45
  • i was just wondering is it correct way to handle user information...stroing it in a variable or using cookie as @sinbar suggested Commented Jul 31, 2019 at 11:02

1 Answer 1

1

You need cookie-session middleware to handle the user information.That is the normal situation in the browser-server architecture. You can get value from session like

app.get('/', function (req, res, next) {
  res.end(req.session.views + ' views')
})

The session Object is a constant obj in the server, which has a default survival time and can be change also. And the cookie-session is a common architecture, whatever nodeJs or java or other server-side language.You may can do an analogy, all behavior is similar.

Sign up to request clarification or add additional context in comments.

3 Comments

i am using express-session for cookie managment // For Passport app.use(session({ secret: 'my key', resave: true, saveUninitialized: true })); // session secret app.use(passport.initialize()); app.use(passport.session()); // persistent login sessions i am not very good with node js.can you please guide how to do with this setup
You requirement is show userinfo at browser right? So you just set the userinfo as a key in session, and set it into response when you need it. I update some code fragment in the answer.
let me try that out.will update the status and thanks for helping me out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.