Skip to content
Snippets Groups Projects
Verified Commit 4dc1ec24 authored by Defendi Alberto's avatar Defendi Alberto
Browse files

Rebase directory structure.

parent ec9c1d13
No related branches found
No related tags found
3 merge requests!56Refined auth flow and new website pages.,!53Feature/authorization/curry,!52Change folder structure and fix auth flow.
import React, { useState, useEffect } from 'react';
import { Route, Redirect, RouteProps } from 'react-router-dom';
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;
};
/**
* Wrapper for Route that basing on if the user is authenticated,
* redirects to:
* - Entry point of the private route (the homepage);
* - Login page.
*/
/* eslint-disable react/jsx-props-no-spreading */
export const RestrictedRoute = ({ Component, path }: Props): JSX.Element => {
const [isAuth, setIsAuth] = useState<boolean>(false);
const [isLoading, setLoading] = useState<boolean>(false);
useEffect(() => {
isAuthenticated().then((res) => {
setIsAuth(res);
setLoading(true);
});
}, [isAuth, setLoading]);
return !isLoading ? (
<CircularProgress />
) : (
<Route
path={path}
render={(props: RouteProps) =>
!isAuth ? (
// Redirect to component.
<Component {...props} />
) : (
// Redirect to homepage.
<Redirect
to={{ pathname: `${AuthRoutes.dashboard}${AuthRoutes.home}` }}
/>
)
}
/>
);
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment