Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom response when credentials are wrong (express) #4

Closed
mauromereu opened this issue Feb 27, 2017 · 2 comments
Closed

Custom response when credentials are wrong (express) #4

mauromereu opened this issue Feb 27, 2017 · 2 comments

Comments

@mauromereu
Copy link

mauromereu commented Feb 27, 2017

Hi, I'm using passport-activedirectory in a rest api, to authenticate for token request.
When an error occurs, i.e. the password is wrong, it always calls a res.end with the error stack as a message and error code 500.
I was able to set the right status (401) in my error managing route in express, but with a workaround:

//error handling
app.use(function(err, req, res, next) {

//here the workaroun
    if (/InvalidCredentialsError/.test(err.stack)) {
      res.status(401);
      return;  // no res.end(mymessage) because it is called by the Strategy,error() of passport-activedirectory
    } 
//end workaround

    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};
    return res.boom.internal(err.message);
}); 

Is there a way to redefine the Strategy.error() method or to avoid it to call res.end, or set a custom message?

@bhoriuchi
Copy link
Owner

bhoriuchi commented Feb 27, 2017

have you tried using the failWithError option?

app.post('/route/to/auth', Passport.authenticate('ActiveDirectory', { failWithError: true}), function (req, res) {
 ...
}, function (error, req, res, next) {
  var statusCode = /InvalidCredentialsError/.test(error.stack)
    ? 401
    : 500
  return res.status(statusCode).send(error.message)
})

@mauromereu
Copy link
Author

This way it works, even without the failWithError option. The key is to define directly in the route the callback for errors.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants