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

Enable custom unauthorized handling via middleware (req, res, next) #23

Open
matmar10 opened this issue May 30, 2019 · 4 comments
Open

Comments

@matmar10
Copy link

Background

Current behavior supports:

  • string (rendered as text in response)
  • object (rendered as JSON object in response)

Enhancement

Enable passing a normal middleware. This allows full customization of the response handling in the case of unauthorized:

  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!'
        });
    }
  }));
@andorfermichael
Copy link

It would be great if this was released. :)

@dazwiafl
Copy link

push push

matmar10 added a commit to matmar10/express-basic-auth that referenced this issue Aug 7, 2021
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
matmar10 added a commit to matmar10/express-basic-auth that referenced this issue Aug 7, 2021
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
matmar10 added a commit to matmar10/express-basic-auth that referenced this issue Aug 7, 2021
@AlphaJuliettOmega
Copy link

Bump?

@LionC
Copy link
Owner

LionC commented Feb 17, 2024

I will take a look at this the following week - thank you for your patience :-)

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 a pull request may close this issue.

5 participants