Created
May 16, 2018 17:23
-
-
Save leandrojo/089e35814fe32156d02a89259609f799 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 from 'react'; | |
| import { Redirect, Route } from 'react-router-dom'; | |
| type Props = { | |
| authed: boolean, | |
| component: React.Component, | |
| }; | |
| class PrivateRoute extends React.Component<void, Props, void> { | |
| state = {}; | |
| render() { | |
| const { component: Component, authed, ...rest } = this.props; | |
| return ( | |
| <Route | |
| {...rest} | |
| render={props => | |
| (authed === true ? ( | |
| <Component {...props} /> | |
| ) : ( | |
| <Redirect | |
| to={{ | |
| pathname: '/login', | |
| state: { from: props.location }, | |
| }} | |
| /> | |
| )) | |
| } | |
| /> | |
| ); | |
| } | |
| } | |
| PrivateRoute.defaultProps = { | |
| location: { | |
| state: {}, | |
| }, | |
| }; | |
| export default PrivateRoute; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment