From 94ab5c3d8f20cd072209c2c2527e31abe1e8df1d Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Sat, 18 Sep 2021 23:36:37 +0200 Subject: [PATCH] Implemented private routes --- src/App.tsx | 58 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 5e20eef..4a9a671 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -4,38 +4,48 @@ import { BrowserRouter as Router, Switch, Route, Redirect } from "react-router-d import NotFound from "./components/NotFound" import Login from "./components/Login" import { useIsAuthQuery } from "./generated/graphql" - -const App: React.FC = () => { - const {data: isAuth, loading } = useIsAuthQuery() - const [loginSuccessful,setLoginSuccessful] = useState(false) +import { useEffect } from "react" - const loginRoutes = ()=>{ - if (loginSuccessful || isAuth?.authorized){ - // Routes available when logged in - return <> - - - - }else{ - // Routes available when NOT logged in - return <> - - { - setLoginSuccessful(true) - }} /> - - - +const App: React.FC = () => { + const { data } = useIsAuthQuery() + const [isAuth,setIsAuth] = useState(false) + const [ready,setReady] = useState(false) + + useEffect(()=>{ + if (data){ + setIsAuth(data.authorized) + setReady(true) } - } + },[data]) return (
{ - !loading && + ready&& - {loginRoutes()} + {/* Private only routes */} + { + isAuth && [ + , + , + , + ] + } + + {/* Public only routes */} + { + !isAuth && [ + + { + setIsAuth(true) + }} /> + , + + ] + } + + {/* Public & private routes */}