title: Sending back a JSON response when failing Passport.js authentication
body: <p>I'm using <code>Node.js</code> as a backend API server for an iPhone client.  I'm using <code>Passport.js</code> to authenticate with a <code>local strategy</code>.  The relevant code is below:</p>
  <pre><code>// This is in user.js, my user model
  UserSchema.static('authenticate', function(username, password, callback) {
      this.findOne({ username: username }, function(err, user) {
          if (err){
              console.log('findOne error occurred');
              return callback(err);
          }
          if (!user){
              return callback(null, false);
          }
          user.verifyPassword(password, function(err, passwordCorrect){
              if (err){
                  console.log('verifyPassword error occurred');
                  return callback(err);
              }
              if (!passwordCorrect){
                  console.log('Wrong password');
                  return callback(err, false);
              }
              console.log('User Found, returning user');
              return callback(null, user);
          });
      });
  });
  </code></pre>
  <p>and</p>
  <pre><code>// This is in app.js
  app.get('/loginfail', function(req, res){
      res.json(403, {message: 'Invalid username/password'});
  });
  
  app.post('/login',
      passport.authenticate('local', { failureRedirect: '/loginfail', failureFlash: false }),
      function(req, res) {
         res.redirect('/');
  });
  </code></pre>
  <p>Right now, I have managed to redirect a failed login to /loginfail, where I send back some JSON to the iPhone client.  However, this doesn't have enough granularity.  I want to be able to send back the appropriate errors to the iPhone client, such as: &quot;No user found&quot; or &quot;Password is wrong&quot;.  With my existing code, I don't see how this can be accomplished.</p>
  <p>I tried to follow the examples for a custom callback on the passport.js site, but I just can't get it to work due to lack of node understanding.  How could I modify my code so that I'd be able to send back a res.json with an appropriate error code/message?</p>
  <p>I am trying something like this now:</p>
  <pre><code>// In app.js
  app.post('/login', function(req, res, next) {
      passport.authenticate('local', function(err, user, info) {
          if (err) { return next(err) }
          if (!user) {
              console.log(info);
              // *** Display message without using flash option
              // re-render the login form with a message
              return res.redirect('/login');
          }
          console.log('got user');
          return res.json(200, {user_id: user._id});
      })(req, res, next);
  });
  
  // In user.js
  UserSchema.static('authenticate', function(username, password, callback) {
      this.findOne({ username: username }, function(err, user) {
          if (err){
              console.log('findOne error occurred');
              return callback(err);
          }
          if (!user){
              return callback(null, false);
          }
          user.verifyPassword(password, function(err, passwordCorrect){
              if (err){
                  return callback(err);
              }
              if (!passwordCorrect){
                  return callback(err, false, {message: 'bad password'});
              }
              console.log('User Found, returning user');
              return callback(null, user);
          });
      });
  });
  </code></pre>
  <p>But back when I try to console.log(info), it just says undefined.  I don't know how to get this custom callback working...Any help would be appreciated!</p>