I am currently working on a login form that prompts users to enter their username, password, and company name. The company name corresponds to the database name, so I need to store this information in the session during the login post request.
In my opinion, storing it in the session
is the better option rather than using req.locals
.
Initially, I attempted to set it with req.locals
but encountered issues:
Here is an excerpt from my app.js file:
// Global Vars
app.use(function (req, res, next) {
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash('error');
res.locals.user = req.user || null;
res.locals.database = req.database || null;
next();
});
// Express Session
app.use(session({
secret: 'secret',
saveUninitialized: true,
resave: true
}));
When attempting to set the database name in the /login
post route like this:
req.locals.database = 'db_xpto';
However, when trying to display the variable in the view, nothing was shown:
h4 Using database: #{database}
What would be the optimal solution for handling this data - utilizing local variables or sessions? And how should I go about setting it?
Note: I am implementing passport
for the login process.