When logging in with the code below in the express controller to redirect to the main page:
req.session.user = user.userLogin;
if (req.session.user=='Admin') {
global.loggedAdmin = user.userLogin;
}
else {
global.loggedUser = user.userLogin;
}
res.redirect('/');
In the main menu, we can check if a user is logged in with the following code:
nav#menu.panel(role='navigation')
ul
li
if loggedUser
li
span.setting Welcome #{loggedUser}
a(href='/account')
i.fa.fa-cog
li
hr
li
a(href='/logout') Logout
li
else if loggedAdmin
li
span.setting Welcome #{loggedAdmin}
a(href='/account')
i.fa.fa-cog
li
hr
li
a(href='/articles') Articles
li
hr
li
a(href='/files') Files
li
hr
li
a(href='/users') Users
li
hr
li
a(href='/logout') Logout
else
li
a(href='/login') Log in
li
hr
li
a(href='/register') Register
In app.js, make sure to include the session setup:
app.use(session({
secret: 'sdfghjkl2345678tyu',
unset:'destroy',
resave: false,
saveUninitialized: false
}));
If there are issues when logging in as an Admin and accessing the website from another device where the menu displays the user as an Admin, it is possible to pass session variables to the Pug template without using globals in the controller. To achieve this, you need to find a way to manage sessions across devices effectively.