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

Add support for custom middleware to handle unauthorized. #24

Closed
wants to merge 14 commits into from

Conversation

matmar10
Copy link

Fixed #23 and maintained backwards compatibility of API.

@matmar10
Copy link
Author

Is this project being actively maintained? Please advise how we can get this PR merged in and others.

matmar10 added 12 commits August 6, 2021 19:48
Current behavior supports:
- string, rendered as text in response
- object, rendered as JSON object in response

This enhancement enables passing a normal middleware. It allows full customization of the response handling in the case of unauthorized:
```JavaScript
  app.use(basicAuth({
    unauthorizedResponse: function(req, res, next) {
      // use an application's existing error handling stack
      if (req.auth) {
        next(new UnauthorizedError('Login required'));
        return;
      }
      // or send an html page
      if (req.accepts('html') {
        res.render('unauthorized');
        return;
      }
      // or whatever you like!
      res.status(403)
        send({
          message: 'That user will NEVER have access. Ever!'
        });
    }
  }));
```

fixes LionC#23
The typescript check seems out of date, since it's currently failing with hundreds of errors. Further investigation is needed to re-enabe this. Better to disable for now and have the build run successfully on the core codebase.
@matmar10
Copy link
Author

matmar10 commented Aug 7, 2021

A fork of this is now being actively maintained & released (which includes this fix). It's published to npm as a scoped package:

npm install --save @blossomfinance/express-basic-auth

@matmar10 matmar10 closed this Aug 7, 2021
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

Successfully merging this pull request may close these issues.

Enable custom unauthorized handling via middleware (req, res, next)
1 participant