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

200
Views
Trying to display different nav items based on if a user is logged in

I am trying to set up an site so that when a user is logged in, the nav bar will display "logout", and if the user isn't logged in, then it will display "signup" and "login". Right now through insomnia I have tested that the login works properly and hits the route. The handlebars just doesn't seem to update based on whether session is loggedIn or not. Any help is much appreciated, and please let me know if you need anymore info on anything.

 {{#if loggedIn}}
            <button class="button is-light" id="logout">logout</button>
 {{else}}
            <a href="http://localhost:3001/signup" class="button is-primary"> <strong>Sign up</strong> </a>
            <a href="http://localhost:3001/login"class="button is-light"> <strong>Log in</strong> </a>
 {{/if}}

This is the route set up for user login

```router.post('/login', async (req, res) => {
`try {
const userData = await User.findOne({
  where: {
    email: req.body.email
  }
})`

if (!userData) {
  res.status(400).json({ message: "Incorrect email or password."});
  return;
}

const validPassword = await userData.checkPassword(req.body.password)

if (!validPassword) {
  console.log("YO")
  res.status(400).json({ message: "Incorrect email or password."})
  return;
}

req.session.save(() => {
  req.session.user_id = userData.id;
  req.session.loggedIn = true;

  res.json({ user: userData, message: 'You are now logged in!' });
});

} catch (err) { res.status(400).json(err)} })

And lastly this is the code server.js code showing using the session data

    const sess = {
    secret: 'Super secret secret',
    cookie: {},
    resave: false,
    saveUninitialized: true,
    store: new SequelizeStore({
    db: sequelize
    })
    };

    app.use(session(sess)); ```

SCRIPT FOR LOGIN PAGE
    const loginFormHandler = async (event) => {
    event.preventDefault();
  
    console.log("YO!!!!")

    const email = document.querySelector('#email-login').value.trim();
    const password = document.querySelector('#password-login').value.trim();
  
    if (email && password) {
      const response = await fetch('/api/users/login', {
        method: 'POST',
        body: JSON.stringify({email, password }),
        headers: { 'Content-Type': 'application/json' },
      });
  
      if (response.ok) {
        // If successful, redirect the browser to the profile page
        document.location.replace('/');
      } else {
        alert(response.statusText);
      }
    }
  };

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!