From a9473a7e41f17b26021306f43fa782b5327e3ef5 Mon Sep 17 00:00:00 2001 From: Vidun-Shanuka-Upek Date: Sat, 21 Mar 2026 20:30:40 +0530 Subject: [PATCH 1/2] fix: properly pass VITE_BACKEND_URL to Vite build in Dockerfile --- crackcode/client/Dockerfile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crackcode/client/Dockerfile b/crackcode/client/Dockerfile index 6424992e..b75e1889 100644 --- a/crackcode/client/Dockerfile +++ b/crackcode/client/Dockerfile @@ -7,11 +7,11 @@ RUN npm install COPY . . -ARG VITE_API_URL=http://localhost:5051 -ENV VITE_API_URL=${VITE_API_URL} - +# Accept backend URL as build argument, default to localhost:5051 ARG VITE_BACKEND_URL=http://localhost:5051 -ENV VITE_BACKEND_URL=${VITE_BACKEND_URL} + +# Create .env.production file with Vite variables +RUN echo "VITE_BACKEND_URL=${VITE_BACKEND_URL}" > .env.production RUN npm run build From e206add4676c446c606b2665a152bb34dec48e3c Mon Sep 17 00:00:00 2001 From: Vidun-Shanuka-Upek Date: Sat, 21 Mar 2026 20:35:31 +0530 Subject: [PATCH 2/2] ensured auth state syncs immediately after login with token change listener and await getuserdata() --- .../userauth/authenticationContext.jsx | 19 ++++++++++++++++++- crackcode/client/src/pages/userauth/Login.jsx | 5 +++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/crackcode/client/src/context/userauth/authenticationContext.jsx b/crackcode/client/src/context/userauth/authenticationContext.jsx index 845de48d..eebcdca5 100644 --- a/crackcode/client/src/context/userauth/authenticationContext.jsx +++ b/crackcode/client/src/context/userauth/authenticationContext.jsx @@ -62,7 +62,24 @@ export const AppContextProvider = (props) => { useEffect(() => { setAuthHeader(); // First, restore auth header from localStorage getAuthState(); // Then check auth status - }, []) + }, []); + + // Listen for token changes in localStorage (e.g., login happens somewhere) + // This ensures all components immediately sync when login occurs + useEffect(() => { + if (typeof window === "undefined") return; + + const handleStorageChange = (e) => { + if (e.key === "accessToken") { + console.log("🔄 Token changed, syncing auth state..."); + setAuthHeader(); // Update axios header + getAuthState(); // Re-check auth status (will call getUserData if logged in) + } + }; + + window.addEventListener("storage", handleStorageChange); + return () => window.removeEventListener("storage", handleStorageChange); + }, []); // Listen for solution submissions to refresh user data globally useEffect(() => { diff --git a/crackcode/client/src/pages/userauth/Login.jsx b/crackcode/client/src/pages/userauth/Login.jsx index 6d47e103..e1d9991b 100644 --- a/crackcode/client/src/pages/userauth/Login.jsx +++ b/crackcode/client/src/pages/userauth/Login.jsx @@ -70,7 +70,8 @@ const Login = () => { } setIsLoggedIn(true); - await getUserData(); + await getUserData(); // Wait for user data to load before navigating + console.log("✅ User logged in and data loaded"); // If the returned user isn't verified, prompt verification flow if (data.user && !data.user.isAccountVerified) { @@ -84,7 +85,7 @@ const Login = () => { navigate("/verify-account"); } else { toast.success("Welcome back!"); - navigate("/home"); + navigate("/home"); // Navigate AFTER auth state + user data are fully loaded } } else { toast.error(data?.message || "Login failed.");