Skip to content

Instantly share code, notes, and snippets.

@barraponto
Created July 3, 2018 14:34
Show Gist options
  • Save barraponto/c5a478469a369196b3f3f806be385262 to your computer and use it in GitHub Desktop.
Save barraponto/c5a478469a369196b3f3f806be385262 to your computer and use it in GitHub Desktop.
import React from "react";
import { Route, Redirect } from "react-router-dom";
import { connect } from "react-redux";
const PrivateRedirect = (props) => (
<Redirect to={{ pathname: "/login", state: { from: props.location } }} />);
const PrivateRoute = ({ component, loggedIn, ...passThroughProps }) => {
const Component = loggedIn ? component : PrivateRedirect;
return <Route component={Component} {...passThroughProps} />;
};
const mapStateToProps = (state, props) => ({ loggedIn: state.loggedIn });
export default connect(mapStateToProps)(PrivateRoute);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment