jaredhanson / passport-http

HTTP Basic and Digest authentication strategies for Passport and Node.js.
https://www.passportjs.org/packages/passport-http/?utm_source=github&utm_medium=referral&utm_campaign=passport-http&utm_content=about
MIT License
268 stars 110 forks source link

Custom error code #55

Closed diego-betto closed 8 years ago

diego-betto commented 8 years ago

Hi, any way to send custom error codes? Something like

passport.use(new BasicStrategy(
  function(userid, password, done) {
    User.findOne({ username: userid }, function (err, user) {
      if (err) { return done(err); }
      if (!user) { return done(null, false,401); }
      if (!user.verifyPassword(password)) { return done(null, false,403); }
      return done(null, user);
    });
  }
));

so for example

Any clue?

Thank you

jaredhanson commented 8 years ago

No. Both of those conditions should be a 401. Why do you want to change it?

diego-betto commented 8 years ago

Since the customer app needs these codes

jaredhanson commented 8 years ago

Fail with an error, and respond with the status code in your error hander.

if (!user.verifyPassword(password)) {
  var err = new Error('Forbidden');
  err.status = 403;
  return done(err);
}
diego-betto commented 8 years ago

Nice works like a charm, changed a little bit since using Restify error.status => error.statusCode

Thank you!