node-auth-csrf
v0.1.1
Published
csrf protection for express application
Downloads
13
Readme
NODE-AUTH-CSRF
INSTALLATION
You can install node-auth-csrf via npm:
npm install node-auth-csrf
USAGE
# Functions
const { csrfProtection } = require('node-auth-csrf');
EXAMPLE
const express = require('express');
const { csrfProtection } = require('node-auth-csrf');
const app = express();
app.use(csrfProtection(process.env.CSRF_SECRET));
app.get("/csrf-token", (req, res) => {
const csrfToken = req.csrfProtection.generateToken();
res.json({ csrfToken });
});
app.get('/protected', (req, res) => {
res.send(req.user);
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
headers: {
'x-csrf-token': 'generated token'
}
GUIDES
app.get('/protected', csrfProtection(process.env.CSRF_SECRET), (req, res) => {
res.send(req.user);
});
OR
app.use('/protected', csrfProtection(process.env.CSRF_SECRET), protectedRoutes);