Last active
August 23, 2019 07:54
-
-
Save codeAdrian/51582f270bf867f95e9e9c23871a6442 to your computer and use it in GitHub Desktop.
React Protected Route Component - based on https://tylermcginnis.com/react-router-protected-routes-authentication/ and extended with redirect prop
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, Redirect } from 'react-router-dom'; | |
const PrivateRoute = ({ | |
component: Component, | |
isAuthenticated, | |
redirectTo, | |
...rest | |
}) => ( | |
<Route | |
{...rest} | |
render={props => | |
isAuthenticated ? ( | |
<Component {...props} /> | |
) : ( | |
<Redirect | |
to={{ | |
pathname: redirectTo ? redirectTo : '/login', | |
state: { from: props.location } | |
}} | |
/> | |
) | |
} | |
/> | |
); | |
export default PrivateRoute; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment