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

229
Views
Unable to show login messages when username is correct and password is wrong

I have just started learning express. I am adding basic authentication using passport js. I have followed this documentation. It is working a little bit odd on error messages. When the username is correct and password is wrong, it doesn't add any messages to session. It just has basic info like path.


    app.post("/login",(req,res)=>{
      const user = new User({
        username:req.body.username,
        password:req.body.password
      });
    
      console.log(req.session);
      req.login(user,function(err){
        if(err){
          console.log(err);
        }
        else{
          passport.authenticate("local",{ failureRedirect: '/login', failureMessage: true })(req,res,function(){
            res.redirect("/secrets");
          });
        }
      });
    
    });

When the username is wrong, it adds message to session but it doesn't reload properly. it doesn't reload at all to "/login". I have to literally kill the process and then this session data is added.

It doesn't seem to work at all. Kindly help me with dealing both of errors, i.e. right username wrong password and wrong username wrong password.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I don't think you use that req.login function correctly.
I think you are misunderstand between login and authentication

What you are thinking is if login fail then authenticate again while actually, you need to authenticate user first, then login after

So the flow, step by step will be:

  1. Verify the user - whether this user entered a correct password or even exist in the system.
    This step called authentication
  2. After you verified the user is what he/she claimed to be. You let the user enter your system. This called login

Here is the document you can follow to understand how passport work.
https://www.passportjs.org/concepts/authentication/downloads/html/ (As you are new to express, make sure you understand the General section thoroughly - Middleware and Strategy are key concepts)
https://www.passportjs.org/tutorials/password/ (this is a practical version of the above document)

over 4 years ago · Santiago Trujillo 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!