I'm trying to create a decently designed JavaScript function that, when passed a function, returns (you guessed it) a function that runs some authentication code before trying to execute the passed in function. So the return function would look something like this...

function createAuthedFunction() {
	return function(variable, numberOf, params) {
		if(checkAuth()) {
			callPassedInFunction(variable, numberOf, params);
		}
	}
}

Obviously I could just call checkAuth in every function that requires it, but that seems very non-DRY to me. What I've ended up going with is this:

function checkAuthFunction(func, error) {
  return function() {
		if (checkAuth()) {
			func.apply(this, arguments);
		} else {
			error = error || 'You must be an admin to do that.';
			console.log(error)
		}
  }
}

Now when an object or function wants to create a method that requires authentication to run, it calls checkAuthFunction and passes in a function (and an optional error message) and this returns a function that checks for proper authentication, and either runs the function or outputs an error message, depending on authentication status. Function.apply allows me to call the passed in function with the current this value and the passed in arguments.

It's working for now, but I'm still not in love with it syntactically.