Skip to content

Instantly share code, notes, and snippets.

@richellyitalo
Last active September 26, 2018 06:00
Show Gist options
  • Save richellyitalo/4b095470a7dd56a749ebf1a0a3d6c4eb to your computer and use it in GitHub Desktop.
Save richellyitalo/4b095470a7dd56a749ebf1a0a3d6c4eb to your computer and use it in GitHub Desktop.
Rota privada utilizando redux.
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