From d477cb8af05a6b8acdb8afa1811777a68e742211 Mon Sep 17 00:00:00 2001 From: realtradam Date: Tue, 18 Jun 2024 22:54:33 -0400 Subject: dynamic login --- src/components/CloseWindow.tsx | 1 + src/components/Layout.tsx | 1 + src/routes/index.tsx | 58 +++++++++++++++++++++++++----------------- 3 files changed, 37 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/components/CloseWindow.tsx b/src/components/CloseWindow.tsx index 247e794..40c5962 100644 --- a/src/components/CloseWindow.tsx +++ b/src/components/CloseWindow.tsx @@ -3,6 +3,7 @@ //export default () => ( export default function Home () { + localStorage.setItem("logged in trigger", String(Math.random())); window.close(); window.location.replace("/"); diff --git a/src/components/Layout.tsx b/src/components/Layout.tsx index 960e112..fc50468 100644 --- a/src/components/Layout.tsx +++ b/src/components/Layout.tsx @@ -8,6 +8,7 @@ export default function Layout ({userData}: userData) const loginLink = () => { window.open(`https://github.com/login/oauth/authorize?client_id=${import.meta.env.VITE_GITHUB_CLIENTID}`); }; + console.log(userData); return ( <>
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