Hoping someone can help me with this. I am using Handlebars as a template engine with Node/Express blog I am building. I am trying to get the login and logout buttons rendered according to when the user is logged in or not. I know I need to use the session somehow but I thought I was using the syntax correctly. I have rendered everything else I was doing but I am stumped on this one.
{{#if sessionSwitch}}
<a class="blog-nav-item" href="/auth/logout">
<p class="nav-fonts">Logout</p>
</a>
{{else}}
<a class="blog-nav-item" href="/auth/login">
<p class="nav-fonts">Login</p>
</a>
{{/if}}
Here is the controller where I am sure I need to use the session.
module.exports = async (req, res) => {
const {
email,
password
} = req.body;
// try to find the user
await User.findOne({
email
}, (error, user) => {
if (user) {
// compare passwords.
bcrypt.compare(password, user.password, (error, same) => {
if (same) {
// store user session.
const sessionSwitch = req.session
sessionSwitch.userId = user._id
res.redirect('/')
console.log({ Message: "Success"})
} else {
res.redirect('/auth/login')
}
})
} else {
res.redirect('/auth/login')
}
})
}