missing basicAuth

This code works fine on 3.5.0, fails with the trace below in 4.0.0-rc3

app.use(express.basicAuth('username', 'password'));
                ^
TypeError: Object function createApplication() {
  var app = function(req, res, next) {
    app.handle(req, res, next);
  };

  mixin(app, proto);
  mixin(app, EventEmitter.prototype);

  app.request = { __proto__: req, app: app };
  app.response = { __proto__: res, app: app };
  app.init();
  return app;
} has no method 'basicAuth'
    at Object.<anonymous> (/home/mman/Projects/MMP2PSession/Server/mmrelay/mmrelay.js:44:17)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

Author: Fantashit

1 thought on “missing basicAuth

  1. @Natim @mman Just posted an example at http://danielstjules.com/2014/08/03/basic-auth-with-express-4/ for future readers

    var basicAuth = require('basic-auth');
    
    /**
     * Simple basic auth middleware for use with Express 4.x.
     *
     * @example
     * app.use('/api-requiring-auth', utils.basicAuth('username', 'password'));
     *
     * @param   {string}   username Expected username
     * @param   {string}   password Expected password
     * @returns {function} Express 4 middleware requiring the given credentials
     */
    exports.basicAuth = function(username, password) {
      return function(req, res, next) {
        var user = basicAuth(req);
    
        if (!user || user.name !== username || user.pass !== password) {
          res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
          return res.send(401);
        }
    
        next();
      };
    };

Comments are closed.