Created
April 30, 2020 22:46
-
-
Save viniciusdacal/798b123d2cf89216c67697e495e1aba9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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, { useContext } from 'react'; | |
import { Route, Redirect } from 'react-router-dom'; | |
import StoreContext from 'components/Store/Context'; | |
const RoutesPrivate = ({ component: Component, ...rest}) => { | |
const { token } = useContext(StoreContext); | |
return ( | |
<Route | |
{...rest} | |
render={({ location }) => token | |
? <Component {...rest} /> | |
: ( | |
<Redirect | |
to={{ | |
pathname: '/login', | |
state: { | |
from: location, | |
redirectOnAuthenticated: true, | |
}, | |
}} | |
/> | |
) | |
} | |
/> | |
) | |
} | |
export default RoutesPrivate; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment