1234567891011121314151617181920212223242526 |
- import React from 'react'
- import PropTypes from 'prop-types'
- import { connect } from 'react-redux'
- import { Route, Redirect, withRouter } from 'react-router-dom'
-
-
-
-
-
- const PrivateRoute = ({ component: Component, ...rest }) => (
- <Route {...rest} render={props => rest.user.isLoggedIn
- ? <Component {...props} />
- : <Redirect to={{pathname: '/login', state: {from: props.location}}} />
- } />
- )
-
- const mapStateToProps = ({ user }) => ({ user })
- export default withRouter(connect(mapStateToProps)(PrivateRoute))
-
- PrivateRoute.propTypes = {
- component: PropTypes.func.isRequired,
- user: PropTypes.shape({
- isLoggedIn: PropTypes.bool.isRequired
- })
- }
|