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

Restricted route component.

parent 9267db45
No related branches found
No related tags found
2 merge requests!56Refined auth flow and new website pages.,!44New route type (RestrictedRoute) and better api calls.
import React, { useState, useEffect, useContext } from 'react';
import { Route, Redirect, RouteProps } from 'react-router-dom';
import { AuthRoutes, NonAuthRoutes } from 'api/routes';
import { isAuthenticated } from 'api/isAuthenticated';
/**
*
* */
type Props = {
Component: React.FC<RouteProps>;
restricted: boolean;
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 [authUser, setAuthUser] = useState<boolean>(false);
useEffect(() => {
isAuthenticated(setAuthUser);
});
return (
<Route
path={path}
render={(props: RouteProps) =>
authUser ? (
// Redirect to homepage.
<Redirect
to={{ pathname: `${AuthRoutes.dashboard}${AuthRoutes.home}` }}
/>
) : (
// Redirect to component.
<Component {...props} />
)
}
/>
);
};
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