Skip to content

Instantly share code, notes, and snippets.

@blackrabbit99
Created December 12, 2016 09:40
Show Gist options
  • Save blackrabbit99/21f407d4528bd9ff3f115cbff812dc32 to your computer and use it in GitHub Desktop.
Save blackrabbit99/21f407d4528bd9ff3f115cbff812dc32 to your computer and use it in GitHub Desktop.
Proxy csrf
const handler = {
get (target, propKey){
if(propKey === 'post'){
return function(...args){
if(!args[args.length - 1].hasOwnProperty('csrfToken') || !args[args.length - 1].hasOwnProperty('cookie')){
return target[propKey](...args)
}
const csrf = args.pop();
return target[propKey](...args)
.set('x-xsrf-token', csrf.csrfToken)
.set('cookie', csrf.cookie);
}
}
return target[propKey];
}
};
request = new Proxy(request, handler);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment