From d477cb8af05a6b8acdb8afa1811777a68e742211 Mon Sep 17 00:00:00 2001 From: realtradam 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 (<> - {/*

{userData.login}

*/} - + {/*

{userData.login}

*/} + - }> - } /> - } /> - } /> - } /> - } /> - } /> - + }> + } /> + } /> + } /> + } /> + } /> + } /> + - - ); +
+ ); } -- cgit v1.2.3