From d477cb8af05a6b8acdb8afa1811777a68e742211 Mon Sep 17 00:00:00 2001
From: realtradam <github@tradam.dev>
Date: Tue, 18 Jun 2024 22:54:33 -0400
Subject: dynamic login

---
 src/routes/index.tsx | 58 +++++++++++++++++++++++++++++++---------------------
 1 file changed, 35 insertions(+), 23 deletions(-)

(limited to 'src/routes/index.tsx')

diff --git a/src/routes/index.tsx b/src/routes/index.tsx
index d653bce..71cfd3b 100644
--- a/src/routes/index.tsx
+++ b/src/routes/index.tsx
@@ -12,30 +12,42 @@ export default function Index()
 {
 	const [userData, setUserData] = useState({ name: '' });
 	useEffect(() => {
-		const url = `${import.meta.env.VITE_API_TITLE}/api/v1/auth/data`;
-		fetch(url, {
-  credentials: "include"
-}).then((response) => {
-		if(response.ok) {
-			return response.json();
-		}
-		throw new Error("Network response was not ok.");
-	}).then((response) => setUserData(response.user_data));}, []);
-		// get user data here
-		// then pass it in as 'props' into the components
+		const update_login_status = () => {
+			console.log("Triggered by login");
+			//localStorage.removeItem("logged in trigger");
+			const url = `${import.meta.env.VITE_API_TITLE}/api/v1/auth/data`;
+			try { fetch(url, {
+				credentials: "include"
+			}).then((response) => {
+				if(response.ok) {
+					return response.json();
+				}
+				//throw new Error("Network response was not ok.");
+			}).then((response) => response && setUserData(response.user_data)).catch((err) => { console.log(err); });}
+			catch(err) { console.log(err); }
+		};
+		window.addEventListener('storage', update_login_status );
+		update_login_status();
+		return () => { window.removeEventListener('storage', update_login_status); };
+	}, []);
+
+
+
+	// get user data here
+	// then pass it in as 'props' into the components
 	return (<>
-		{/*<h1>{userData.login}</h1>*/}
-		<Router>
+			{/*<h1>{userData.login}</h1>*/}
+			<Router>
 			<Routes>
-				<Route path="/" element = {<Layout userData={userData}/>}>
-					<Route index element={<Home />} />
-					<Route path="/blogs" element={<Blogs />} />
-					<Route path="/games" element={<Games />} />
-					<Route path="/games/upload" element={<UploadGame />} />
-					<Route path="/game/:game" element={<Game />} />
-					<Route path="/closewindow" element={<CloseWindow />} />
-				</Route>
+			<Route path="/" element = {<Layout userData={userData}/>}>
+			<Route index element={<Home />} />
+			<Route path="/blogs" element={<Blogs />} />
+			<Route path="/games" element={<Games />} />
+			<Route path="/games/upload" element={<UploadGame />} />
+			<Route path="/game/:game" element={<Game />} />
+			<Route path="/closewindow" element={<CloseWindow />} />
+			</Route>
 			</Routes>
-		</Router>
-	</>);
+			</Router>
+			</>);
 }
-- 
cgit v1.2.3

