Created
December 22, 2013 14:46
-
-
Save A/8083590 to your computer and use it in GitHub Desktop.
middleware basicAuth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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