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

118
Views
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 answers
Answer question

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 Report
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!