Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
How do I render a pug template on success and failure of authentication(passport.js) with variables from the pug template? (express, passport.js, pug)

I am working on the login system using passport.js and express with pug templates. I am trying to render the login page with a message on failure, and if on success I want to redirect the user back to the last page they have been to, of which URL is passed as hidden input inside the pug template. I want to then take that value of that hidden input and put it inside the successRedirect, however it says req is not defined:

router.post("/login", body('backurl').trim().escape(), passport.authenticate("local", {
    successRedirect: (req.body.backurl),
    failWithError: true
}), function(err, req, res, next) {
    return res.render('login', { message: 'Unable to login, the password or the username are wrong' })
}

from my understanding, that happens because of it not having a req since there is no function with req, res, next, so I tried to wrap it with a function that has it:

router.post("/login",
    body('backurl').trim().escape(),
    function(req, res, next) {
        passport.authenticate("local", {
            successRedirect: (req.body.backurl),
            failWithError: true
        }), function(err, req, res, next) {
            return res.render('login', {message: 'Unable to login, the password or the username are wrong'})
        }
    }
);

but it just keeps reloading and doesn't work and doesn't go to any URL.

can anybody help me fix this and move the user to the URL from the hidden input while rendering a pug template on failure with variables?

Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can render a view with a get() request. With a post() request you should redirect and if you want to show a message to the user you could use the express-flash package.

router.post("/login", (req, res, next) => {
   passport.authenticate("local", (err, user) => {
        if (!user) {
          req.flash('error', { msg: 'Unable to login, the password or the username are wrong')
          return res.redirect('/login');
        }
        res.redirect(req.body.backurl);
   }
});

This way you can have more details about what appened.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda