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

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

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