From c0b2095293b5e0c231d213883fadff58bbf92951 Mon Sep 17 00:00:00 2001 From: Alberto Defendi <1369-ahl-berto@users.noreply.gitlab.inf.unibz.it> Date: Sun, 23 May 2021 15:34:01 +0200 Subject: [PATCH] Redirect to dashboard if user is auth. --- .../RestrictedRoute/RestrictedRoute.tsx | 25 +++++-------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/src/components/RestrictedRoute/RestrictedRoute.tsx b/src/components/RestrictedRoute/RestrictedRoute.tsx index 3be9157..045f868 100644 --- a/src/components/RestrictedRoute/RestrictedRoute.tsx +++ b/src/components/RestrictedRoute/RestrictedRoute.tsx @@ -4,11 +4,10 @@ import { AuthRoutes } from 'api/routes'; import { isAuthenticated } from 'api/isAuthenticated'; import { CircularProgress } from '@material-ui/core'; -/** - * - * */ type Props = { + /** Children where the authenticated user will be redirected. */ Component: React.FC<RouteProps>; + /** Path of the children. */ path: string; }; @@ -25,23 +24,11 @@ export const RestrictedRoute = ({ Component, path }: Props): JSX.Element => { const [isLoading, setLoading] = useState<boolean>(false); useEffect(() => { - let isMounted = true; - - isAuthenticated().then((state) => { - if (isMounted) { - setIsAuth(state); - setLoading(true); - } - }); - - return () => { - isMounted = false; - }; - }, [isLoading]); + isAuthenticated().then((res) => setIsAuth(res)); + console.log(isAuth); + }, [isAuth]); - return !isLoading ? ( - <CircularProgress /> - ) : ( + return ( <Route path={path} render={(props: RouteProps) => -- GitLab