Created
October 26, 2017 13:12
-
-
Save tapandave08/fd00a6e6792e17a76b952cf42cea7a4e to your computer and use it in GitHub Desktop.
require-authentication
This file contains hidden or 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 { connect } from "react-redux"; | |
import PropTypes from "prop-types"; | |
import history from '../../components/utility/history'; | |
export default function(ComposedComponent) { | |
class Authentication extends Component { | |
static contextTypes = { | |
router: PropTypes.object | |
}; | |
componentWillMount() { | |
if (!this.props.authenticated) { | |
history.push("/"); | |
} | |
} | |
componentWillUpdate(nextProps) { | |
if (!nextProps.authenticated) { | |
history.push("/"); | |
} | |
} | |
render() { | |
return <ComposedComponent {...this.props} />; | |
} | |
} | |
function mapStateToProps(state) { | |
return { authenticated: state.auth.isauth }; | |
} | |
return connect(mapStateToProps)(Authentication); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment