LionC / express-basic-auth

Plug & play basic auth middleware for express
328 stars 57 forks source link

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

Open matmar10 opened 5 years ago

matmar10 commented 5 years ago

Background

Current behavior supports:

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 commented 4 years ago

It would be great if this was released. :)

dazwiafl commented 4 years ago

push push

AlphaJuliettOmega commented 2 years ago

Bump?

LionC commented 7 months ago

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