Last active
September 26, 2018 06:00
-
-
Save richellyitalo/4b095470a7dd56a749ebf1a0a3d6c4eb to your computer and use it in GitHub Desktop.
Rota privada utilizando redux.
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, { Component } from 'react' | |
import { connect } from 'react-redux' | |
import PropTypes from 'prop-types' | |
import { Route, Redirect } from 'react-router-dom' | |
/* | |
* Como usar: | |
* <Switch> | |
* <PrivateRoute exact path="/comp" to={Comp} /> | |
* </Switch> | |
*/ | |
const PrivateRoute = ({ component: Component, auth, ...rest }) => ( | |
<Route | |
{...rest} | |
render={props => | |
auth.isAuthenticated === true ? ( | |
<Component {...props} /> | |
) : ( | |
<Redirect to="/login" /> | |
) | |
} | |
/> | |
) | |
PrivateRoute.propTypes = { | |
auth: PropTypes.object.isRequired | |
} | |
const mapStateToProps = state => ({ | |
auth: state.auth | |
}) | |
export default connect(mapStateToProps)(PrivateRoute) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment