Created
April 8, 2020 17:24
-
-
Save Porter97/f5d6648bb92747dd63c029d83a523d5b to your computer and use it in GitHub Desktop.
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 { 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