import React from 'react' import PropTypes from 'prop-types' import { connect } from 'react-redux' import { Route, Redirect, withRouter } from 'react-router-dom' // component inspired from https://reacttraining.com/react-router/web/example/auth-workflow // /!\ you shall destruct props.component otherwise you get a warning: // "You should not use and in the same route; will be ignored const PrivateRoute = ({ component: Component, ...rest }) => ( rest.user.isLoggedIn ? : } /> ) const mapStateToProps = ({ user }) => ({ user }) export default withRouter(connect(mapStateToProps)(PrivateRoute)) PrivateRoute.propTypes = { component: PropTypes.func.isRequired, user: PropTypes.shape({ // user is get with redux connect isLoggedIn: PropTypes.bool.isRequired }) }