Skip to content

Instantly share code, notes, and snippets.

@geek0x23
Created December 4, 2011 23:41
Show Gist options
  • Save geek0x23/1431671 to your computer and use it in GitHub Desktop.
Save geek0x23/1431671 to your computer and use it in GitHub Desktop.
A wrapper for the default connect/express csrf middleware that adds ignore support
var express = require('express'),
_ = require('underscore'),
parse = require('url').parse;
exports = module.exports = load;
exports.ignore = ['/api/method1', '/api/method2'];
function load(options) {
var defaultCsrf = express.csrf(options);
return function load(req, res, next) {
if (exports.ignore.length > 0) {
var reqPath = parse(req.url).pathname,
ignore = false;
_.each(exports.ignore, function(ignorePath) {
if (~reqPath.indexOf(ignorePath)) {
ignore = true;
return;
}
});
}
if (ignore) {
return next();
} else {
return defaultCsrf(req, res, next);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment