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 */}