jaredhanson/passport — pull request #168 by d1b1 (2013-09-20 15:00:00+00:00)
Title: Enhanced the res.end('Unauthorized') response

I have found that when using Passport within an app does not handle errors, like an API, I have little way to communicate helpful error messages to the endpoint request. This would make the 401 - Unauthorized response a bit more robust.

Example for Local Strategy:

passport.use("local", new LocalStrategy(function(username, password, done) {

store.findUserByUsername(username, function(err, user) {
if (err)
return done(null, false, { name: 'Error', message: err.message } );

if (!user) {
return done(null, false, { name: 'NoAccountFound', message: 'Invalid Username and Password.' } );
} else {
done(null, user);
}

});
}
));

Returning:

401

[
{
"name": "NoAccountFound",
"message": "Invalid Username and Password."
}
]