Skip to content

Instantly share code, notes, and snippets.

@Nilanth
Last active August 5, 2021 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nilanth/e484fd84079c70f0a12601f0402f6d25 to your computer and use it in GitHub Desktop.
Save Nilanth/e484fd84079c70f0a12601f0402f6d25 to your computer and use it in GitHub Desktop.
Private Route
import {
Route,
Redirect
} from 'react-router-dom';
function PrivateRoute({ children, isAuthenticated, ...rest }) {
return (
<Route
{...rest}
render={
({ location }) => (
isAuthenticated
? (
children
) : (
<Redirect
to={{
pathname: '/login',
state: { from: location }
}}
/>
))
}
/>
);
}
export default PrivateRoute;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment