Skip to content

Instantly share code, notes, and snippets.

@Porter97
Created April 8, 2020 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Porter97/f5d6648bb92747dd63c029d83a523d5b to your computer and use it in GitHub Desktop.
Save Porter97/f5d6648bb92747dd63c029d83a523d5b to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import { Route, Redirect, withRouter } from 'react-router-dom';
import { connect } from 'react-redux';
const mapStateToProps = (state) => ({
currentUser: state.common.currentUser
});
class AuthedRoute extends Component {
render() {
const { component, ...rest } = this.props;
const Component = component;
return (
<Route
{...rest}
render={(props) => {
if (!localStorage.getItem('currentUser')) {
if (this.props.redirect) {
return <Redirect to="/register" />;
} else {
return <div />;
}
} else if (!this.props.currentUser) {
console.log(1);
return <div />;
} else {
return <Component {...props} />;
}
}}
/>
);
}
}
AuthedRoute.defaultProps = {
redirect: true,
};
export default withRouter(connect(mapStateToProps)(AuthedRoute));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment