fix cookie

This commit is contained in:
krikorios
2025-06-09 01:55:55 +03:00
parent 83ffdf860a
commit 31b54a1ecc
13 changed files with 1659 additions and 1816 deletions
+18
View File
@@ -3,6 +3,8 @@ import express, { type Request, Response, NextFunction } from "express";
import { registerRoutes } from "./routes";
import { taskScheduler } from "./services/taskScheduler";
import { setupVite, serveStatic, log } from "./vite";
import session from 'express-session';
import passport from 'passport';
const app = express();
app.set('trust proxy', true);
@@ -24,6 +26,22 @@ app.use((req, res, next) => {
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// Session middleware
app.use(session({
secret: process.env.SESSION_SECRET || 'default_secret',
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: true,
secure: app.get('env') === 'production',
maxAge: 1000 * 60 * 60 * 24, // 1 day
},
}));
// Passport middleware
app.use(passport.initialize());
app.use(passport.session());
app.use((req, res, next) => {
const start = Date.now();
const path = req.path;
+1 -1
View File
@@ -57,7 +57,7 @@ export const securityMiddleware = helmet({
defaultSrc: ["'self'"],
styleSrc: ["'self'", "'unsafe-inline'", "https://fonts.googleapis.com"],
fontSrc: ["'self'", "https://fonts.gstatic.com"],
scriptSrc: ["'self'", "'unsafe-inline'", "https://replit.com"],
scriptSrc: ["'self'", "'unsafe-inline'"],
imgSrc: ["'self'", "data:", "https:"],
connectSrc: ["'self'", "ws:", "wss:"]
}
+1 -1
View File
@@ -84,7 +84,7 @@ export async function registerRoutes(app: Express): Promise<Server> {
cookie: {
secure: false, // Set to true in production with HTTPS
httpOnly: true,
maxAge: 24 * 60 * 60 * 1000, // 24 hours
maxAge: 30 * 24 * 60 * 60 * 1000, // 30 days for development
sameSite: 'lax' // Allow cross-origin requests for development
}
}));
+10 -1
View File
@@ -23,10 +23,12 @@ export function log(message: string, source = "express") {
}
export async function setupVite(app: Express, server: Server) {
log("🔧 Setting up Vite development server...");
const serverOptions = {
middlewareMode: true,
hmr: { server },
allowedHosts: true,
allowedHosts: true as true,
};
const vite = await createViteServer({
@@ -43,9 +45,12 @@ export async function setupVite(app: Express, server: Server) {
appType: "custom",
});
log("✅ Vite server created, adding middleware...");
app.use(vite.middlewares);
log("✅ Vite middleware added");
app.use("*", async (req, res, next) => {
const url = req.originalUrl;
log(`🌐 Handling request for: ${url}`);
try {
const clientTemplate = path.resolve(
@@ -55,15 +60,19 @@ export async function setupVite(app: Express, server: Server) {
"index.html",
);
log(`📄 Reading template from: ${clientTemplate}`);
// always reload the index.html file from disk incase it changes
let template = await fs.promises.readFile(clientTemplate, "utf-8");
template = template.replace(
`src="/src/main.tsx"`,
`src="/src/main.tsx?v=${nanoid()}"`,
);
log(`🔄 Transforming HTML with Vite...`);
const page = await vite.transformIndexHtml(url, template);
log(`✅ Sending transformed HTML`);
res.status(200).set({ "Content-Type": "text/html" }).end(page);
} catch (e) {
log(`❌ Error processing request: ${e}`);
vite.ssrFixStacktrace(e as Error);
next(e);
}