Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export const checkAuth = (ChildComponent) => {
- class Authenticate extends React.Component {
- componentWillMount() {
- if(!this.props.isAuthenticated) {
- console.log("You need to be authenticated to access");
- this.context.router.push('/login');
- }
- }
- componentWillUpdate() {
- if(!this.props.isAuthenticated) {
- console.log("You need to be authenticated to access");
- this.context.router.push('/login');
- }
- }
- render() {
- return (
- <ChildComponent {...this.props} />
- )
- }
- }
- Authenticate.propTypes = {
- isAuthenticated: React.PropTypes.bool.isRequired
- };
- Authenticate.contextTypes = {
- router: React.PropTypes.object.isRequired
- };
- function mapStateToProps(state) {
- return {
- isAuthenticated: state.auth.isAuthenticated
- }
- }
- return connect(mapStateToProps)(Authenticate);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement