fixed bug where had to login every time the server restarted
This commit is contained in:
@@ -1,11 +1,24 @@
|
|||||||
const csrf = require("csrf");
|
const csrf = require("csrf");
|
||||||
const cookieParser = require("cookie-parser");
|
const cookieParser = require("cookie-parser");
|
||||||
|
const logger = require("../utils/logger");
|
||||||
|
|
||||||
// Initialize CSRF token generator
|
// Initialize CSRF token generator
|
||||||
const tokens = new csrf();
|
const tokens = new csrf();
|
||||||
|
|
||||||
// Generate a secret for signing tokens
|
// Use persistent secret from environment variable to prevent token invalidation on restart
|
||||||
const secret = tokens.secretSync();
|
const secret = process.env.CSRF_SECRET;
|
||||||
|
|
||||||
|
if (!secret) {
|
||||||
|
const errorMsg = "CSRF_SECRET environment variable is required.";
|
||||||
|
logger.error(errorMsg);
|
||||||
|
throw new Error(errorMsg);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (secret.length < 32) {
|
||||||
|
const errorMsg = "CSRF_SECRET must be at least 32 characters for security";
|
||||||
|
logger.error(errorMsg);
|
||||||
|
throw new Error(errorMsg);
|
||||||
|
}
|
||||||
|
|
||||||
// CSRF middleware using double submit cookie pattern
|
// CSRF middleware using double submit cookie pattern
|
||||||
const csrfProtection = (req, res, next) => {
|
const csrfProtection = (req, res, next) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user