Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda