From fbdc1ff1dac973a9309e45a7fbe0375dd3031ff9 Mon Sep 17 00:00:00 2001 From: ghaddaditw <40211818-ghaddaditw@users.noreply.replit.com> Date: Sun, 8 Jun 2025 14:51:14 +0000 Subject: [PATCH] Ensure user sessions persist correctly and securely across the platform Implements CORS and updates AuthContext to manage user authentication via cookie-based sessions. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 8fac8604-8776-4090-bf16-1e3d01acb719 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/9777c70b-fc38-4831-8d6b-78dfffe041b0/e43769e0-ea3b-4deb-b53a-a84e46de587b.jpg --- client/src/context/AuthContext.tsx | 17 ++++++++++++++--- server/index.ts | 15 +++++++++++++++ test_cookies.txt | 5 +++++ 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 test_cookies.txt diff --git a/client/src/context/AuthContext.tsx b/client/src/context/AuthContext.tsx index 18c7948..17ad9db 100644 --- a/client/src/context/AuthContext.tsx +++ b/client/src/context/AuthContext.tsx @@ -27,10 +27,21 @@ export function AuthProvider({ children }: { children: React.ReactNode }) { const checkAuthStatus = async () => { try { - const currentUser = await authService.getCurrentUser(); - setUser(currentUser); + const response = await fetch('/api/auth/me', { + credentials: 'include', + headers: { + 'Content-Type': 'application/json', + }, + }); + + if (response.ok) { + const data = await response.json(); + setUser(data.user); + } else { + setUser(null); + } } catch (error) { - // User not authenticated + console.warn('Auth check failed:', error); setUser(null); } finally { setIsLoading(false); diff --git a/server/index.ts b/server/index.ts index 10c7e1d..f432e44 100644 --- a/server/index.ts +++ b/server/index.ts @@ -5,6 +5,21 @@ import { setupVite, serveStatic, log } from "./vite"; const app = express(); app.set('trust proxy', true); + +// CORS configuration for authentication +app.use((req, res, next) => { + res.header('Access-Control-Allow-Credentials', 'true'); + res.header('Access-Control-Allow-Origin', req.headers.origin); + res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); + res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With'); + + if (req.method === 'OPTIONS') { + res.sendStatus(200); + } else { + next(); + } +}); + app.use(express.json()); app.use(express.urlencoded({ extended: false })); diff --git a/test_cookies.txt b/test_cookies.txt new file mode 100644 index 0000000..22f8dda --- /dev/null +++ b/test_cookies.txt @@ -0,0 +1,5 @@ +# Netscape HTTP Cookie File +# https://curl.se/docs/http-cookies.html +# This file was generated by libcurl! Edit at your own risk. + +#HttpOnly_localhost FALSE / FALSE 1749480636 connect.sid s%3Aj7g6cTWTz7BCYrKu889ssWBXcXcW1o64.OY0iia0%2B%2Bpuriroxk01ON7%2FX%2FqMAS0NBpglNNJ0a%2F6M