csrf-monkey
Automatically add CSRF headers to all clientside requests
- handles both xhr and fetch
- small footprint, no dependencies
- configurable, testable and restorable
Installation
npm install --save csrf-monkey
Usage
Default behaviour
Put your csrf token in a meta tag in your head like so:
<html>
<head>
<meta name='csrf-token' content='value'>
</head>
<body></body>
</html>
Then call csrf-monkey
. This will patch xhr and window.fetch so that your csrf token is automatically included in all clientside requests
var axios = require('axios')
require('csrf-monkey')()
fetch('/api')
axios.get('/api')
Options
var csrfMonkey = require('csrf-monkey')
csrfMonkey(header, token)
csrfMonkey('my-custom-csrf-header')
csrfMonkey(null, 'my-csrf-token')
Restore
var restore = csrfMonkey()
restore()
Credits