title: Understanding done function in PassportJS
body: <p>I am trying to implement a normal login and signup module and I am facing difficulty in understanding the done function.<a href="https://github.com/jaredhanson/passport-local/blob/master/lib/strategy.js#L80" rel="nofollow">here</a> is the line I feel is responsible for the done function. Now what I wanted to do was return the corresponding messages like if there was some server error.</p>
  
  <pre><code>app.post('/user/auth/login', passport.authenticate('local-login'), function(req, res) {
      console.log("Login Request Successful");
      if(req.user){
          res.status(200).send(JSON.stringify({
              'msg' : "Successfully logged in"
          }));
      }
      else{
          res.status(400)
      }
      //console.log(req.user);
  });
  </code></pre>
  
  <p>This is for the case when after logging in Passport attaches a user object to the request module. 
  But how do I differentiate between a server error and authentication failed error </p>
  
  <p>Here is my authenticate middleware. </p>
  
  <pre><code>passport.use('local-login', new LocalStrategy({
      passReqToCallback: true
  }, function(req, username, password, done) {
  
      Users.findOne({
          'emailId': username
      }, function(err, user) {
          if (err)
              return done(err);
          if (!user)
              return done(null, false);
          else if (passwordHash.verify(password, user.password)) {
              console.log("User is verified");
              req.session.save();
              return done(null, user);
  
          } else
              return done(null, false);
      });
  }));
  </code></pre>
  
  <p>Basically, I need to access the messages in done() function. How do I do that? </p>
  
  <p>How does function, like if I type the wrong password, the message I get in my browser in Unauthorized. It means somewhere it is setting the response.data field to Unauthorized. Instead of that I want to know when there is an error and want to send my custom message.</p>