Skip to content

Instantly share code, notes, and snippets.

@A
Created December 22, 2013 14:46
Show Gist options
  • Save A/8083590 to your computer and use it in GitHub Desktop.
Save A/8083590 to your computer and use it in GitHub Desktop.
middleware basicAuth
'use strict';
/**
* Module dependencies.
*/
var log = require('winston-wrapper')(module);
var config = require('nconf');
var express = require('express');
// End of dependencies.
// Без флага authRequired проверяет, есть ли `req.headers.authorization`, если нет — ничего не делает,
// если есть — проверяет корректность авторизационных данных.
// С флагом authRequired требует авторизации для доступа к странице.
module.exports = function (credentials, authRequired) {
return authRequired
? express.basicAuth.apply(credentials)
: function (req, res, next) {
credentials.push(next);
var mw = express.basicAuth.apply(credentials);
req.headers.authorization
? mw(req, res, next)
: next();
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment