Created
July 28, 2020 16:46
-
-
Save AZagatti/35337c7953493f3f31369b507747227c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { | |
Route as ReactDOMRoute, | |
RouteProps as ReactDOMRouteProps, | |
Redirect, | |
} from 'react-router-dom'; | |
import { useAuth } from '../hooks/auth'; | |
interface RouteProps extends ReactDOMRouteProps { | |
isPrivate?: boolean; | |
component: React.ComponentType; | |
} | |
const Route: React.FC<RouteProps> = ({ | |
isPrivate = false, | |
component: Component, | |
...rest | |
}) => { | |
const { user } = useAuth(); | |
// You can store user data in another way and only retrieve it here | |
return ( | |
<ReactDOMRoute | |
{...rest} | |
render={({ location }) => { | |
return isPrivate === !!user ? ( | |
<Component /> | |
) : ( | |
<Redirect | |
to={{ | |
pathname: isPrivate ? '/' : '/dashboard', | |
state: { from: location }, | |
}} | |
/> | |
); | |
}} | |
/> | |
); | |
}; | |
export default Route; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment