i’ve been trying for a while to get the tabs on my navigation bar to change (including the link) when a user logs in. The whole thing is a problem on the index page, as you should enter it both logged in and not logged in. i have a in my navigation bar where all tabs are listed via an anchor. This is my app.get of the index:
app.get('/', (req, res)=>{ const {userId} = req.session; if(userId==undefined){ res.render('index'); } else{ res.render('index'); } });
and this Is my header :
<nav> <div class="logo">Logo</div> <div class="openMenu"><i class="fa fa-bars"></i></div> <ul class="mainMenu"> <li><a href="./">Startseite</a></li> <li><a href="./login">Anmelden</a></li> <li><a href="./registrieren">Registrieren</a></li> <li><a href="#">Impressum</a></li> </ul> <div class="closeMenu"><i class="fa fa-times"></i></div> </nav>
I want to change “Anmelden” to “Log out” with this :
<li><form id="abmelden" method="POST" action="/logout"><a href="./" onclick="abmelden()">Abmelden</a></form></li>
And I want to change “Registrieren” to “Dashboard” therefore the link of the a tag and the text must be changed.
Is there a way, where I can use a command like res.send() but to send the depends on, if the user is logged or not
Advertisement
Answer
One common technique is to dynamically insert a class on the body tag that is either loggedIn or notLoggedIn. You can then control the rest of your page purely with CSS.
You just insert both the loggedIn and not loggedIn tabs in your navbar and use CSS so one of them is always hidden based on what class is in the tag:
<body class="loggedIn">
or
<body class="notLoggedIn">
Then, you use the same page for both, but use CSS to hide/show things based on the state in the tag. You use conditionals in EJS to insert the right class for the tag based on the logged in state.
You can do this in your index.html page with an EJS conditional to dynamically construct the <body>
tag with the right class in it:
<body <% if (loggedIn) { %> class="loggedIn" > <%} else { %> class="loggedOut" > <% } %>
I’m not an expert on EJS so if this isn’t perfect EJS syntax, you can hopefully see the concept here. When you render your template, you need to pass the loggedIn
boolean to the rendering engine so it can dynamically adjust based on that.
Then, you add CSS that shows or hides:
/* hide/show based on login status */ .loggedIn .whenLoggedIn, .loggedOut .whenLoggedOut {display: block;} .loggedOut .whenLoggedIn, .loggedIn .whenLoggedOut {display: none;}
Then, you put both tabs in your navbar and the class
in the <body>
tag combined with your CSS will hide/show one of them
<nav> <div class="logo">Logo</div> <div class="openMenu"><i class="fa fa-bars"></i></div> <ul class="mainMenu"> <li><a href="./">Startseite</a></li> <li class="whenLoggedOut"><a href="./login">Anmelden</a></li> <li class="whenLoggedIn"><a href="./logout">Ausloggen</a></li> <li><a href="./registrieren">Registrieren</a></li> <li><a href="#">Impressum</a></li> </ul> <div class="closeMenu"><i class="fa fa-times"></i></div> </nav>
Note: You could also use EJS conditionals to just generate the desired tab also so just one tab was generated. But, the technique shown here creates one EJS conditional and then everything else is just HTML/CSS which some people find to be simpler to manage. So, you could have multiple places in the page that have loggedIn and notLoggedIn HTML, but they all work off the class/CSS generates from the one EJS conditional.