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

291
Visualizações
How do I render different pages with variables on post request of login with passport.js local authentication?(Express)

I am trying to login the user with passport.js authentication and on success I redirect the user to the last page that comes from a variable inside the login page, and on failure I re-render the login page with a message, I tried to do it but I fail, here what I did:

router.post('/login', 
  body('backurl').trim().escape(),

  (req,res,next) =>{
    passport.authenticate('local', (err,user)=>{
      if(err){return next(err)}
      if(!user){
        return res.render('login', {message: 'Unable to login, the password or the username are wrong'})
      }
      if(req.body.backurl == null){
        return res.redirect('/yourcourses')
      }
      return res.redirect(req.body.backurl)
    });
  }
); 

it just keeps endlessly loading and nothing comes out, what is wrong with my code?

Thanks!

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

0

I can't seem to find a definitive reference for passport.authenticate, but all the example in https://www.passportjs.org/concepts/authentication/ show it as a middleware, so I think you'll need to use it as such:

router.post('/login', 
  passport.authenticate('local'),
  (req, res, next) => {
    req.body('backurl').trim().escape();
    if (!req.user) {
      return res.render('login', {
        message: 'Unable to login, the password or the username are wrong'
      });
    }
    if (req.body.backurl == null) {
      return res.redirect('/yourcourses');
    }
    
    return res.redirect(req.body.backurl);
  }
); 
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