Skip to content

Commit

Permalink
Added csurf and cookie-parser Dependencies and Added Generate CSRF To…
Browse files Browse the repository at this point in the history
…ken Feature
  • Loading branch information
stanleyowen committed Mar 15, 2021
1 parent f80ebf5 commit a00218d
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
15 changes: 15 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
const cors = require('cors');
const csrf = require('csurf');
const helmet = require('helmet');
const express = require('express');
const passport = require('passport');
const mongoose = require('mongoose');
const cookieParser = require('cookie-parser');

const app = express();
const PORT = process.env.PORT || 5000;
const csrfProtection = csrf({ cookie: true })

require('dotenv').config();
require('./config/passport');

app.use(cors());
app.use(helmet());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.json());
app.use(passport.initialize());

app.use(csrfProtection, (req, res, next) => {
var token = req.csrfToken();
res.cookie('XSRF-TOKEN', token);
res.locals.csrfToken = token;
next();
});

app.get('/csrf', csrfProtection, (req, res) => {
res.json({'XSRF-TOKEN': req.csrfToken()});
});

const usersRouter = require('./routes/users.route');
const todoRouter = require('./routes/todo.route');
const statusRouter = require('./routes/status.route');
Expand Down
155 changes: 155 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
"dependencies": {
"axios": "^0.21.1",
"bcrypt": "^5.0.1",
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
"crypto": "^1.0.1",
"csurf": "^1.11.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"helmet": "^4.4.1",
Expand Down

0 comments on commit a00218d

Please sign in to comment.