Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
Using Handlebars with Node.js/Express, need login and logout to render according to user signing in

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')
    }
})

}

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!