Skip to content

Instantly share code, notes, and snippets.

@rob-mcgrail
Last active September 6, 2017 02:30
Show Gist options
  • Save rob-mcgrail/d72905741cce9e804fad1551195b63e0 to your computer and use it in GitHub Desktop.
Save rob-mcgrail/d72905741cce9e804fad1551195b63e0 to your computer and use it in GitHub Desktop.
import { connect } from 'react-redux';
import HOCPrivateRoute from 'src/components/utility/PrivateRoute';
function mapStateToProps(state) {
return {
authenticated: state.auth.authenticated,
redirectTo: '/welcome'
};
}
const PrivateRoute = connect(mapStateToProps)(HOCPrivateRoute);
export default PrivateRoute;
import React from 'react';
import PropTypes from 'prop-types';
import { Route, Redirect, withRouter } from 'react-router-native';
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route
{...rest}
render={props => (
rest.authenticated ? (
<Component {...props} />
) : (
<Redirect to={{
pathname: rest.redirectTo,
state: { from: props.location }
}}
/>
)
)}
/>
);
PrivateRoute.propTypes = {
component: PropTypes.func.isRequired,
location: PropTypes.object.isRequired
};
export default withRouter(PrivateRoute);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment