-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
52 lines (44 loc) · 1.37 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require("dotenv").config();
const express = require("express");
const cluster = require("cluster");
const app = express();
const totalCPUs = require("node:os").cpus().length;
const session = require("express-session");
const csrf = require("csurf");
const csrfProtect = csrf({ cookie: true });
var cookieParser = require("cookie-parser");
var bodyParser = require("body-parser");
var parseForm = bodyParser.urlencoded({ extended: false });
const sessionConfig = session({
secret: process.env.CSRFT_SESSION_SECRET,
keys: ["some random key"],
resave: false,
saveUninitialized: false,
cookie: {
maxAge: parseInt(process.env.CSRFT_EXPIRESIN),
sameSite: "strict",
httpOnly: true,
domain: process.env.DOMAIN,
secure: false,
},
});
app.use(cookieParser());
app.use(sessionConfig);
///Routes
const user = require("./controllers/user");
app.get("/", (req, res) => {
res.send("Hello World");
});
app.get("/api/setCSRFToken", csrfProtect, (req, res, next) => {
const token = req.csrfToken();
res.send({ csrfToken: token });
});
app.post("/api/checkCSRFToken", parseForm, csrfProtect, function (req, res) {
res.send({ msg: "CSRF Token is valid." });
});
// // app.use(csrfProtect);
app.post("/api/login", user.login);
app.post("/api/logout", user.signout);
app.listen(process.env.PORT, () => {
console.log(`Example app listening on port ${process.env.PORT}`);
});