Created
July 21, 2016 22:30
-
-
Save ooade/fc6cc0ea800fc676b08c1a159e673494 to your computer and use it in GitHub Desktop.
Main HOC
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'; | |
// Grab the {createContainer} from react-meteor-data pckg and react-router pckg | |
import { createContainer } from 'meteor/react-meteor-data'; | |
import { browserHistory } from 'react-router'; | |
// Wrap our component in a function with the ComposedComponent as an argument | |
// ComposedComponent is the component to be rendered | |
export default function(ComposedComponent) { | |
class RequireAuth extends Component { | |
componentWillMount() { | |
if (!this.props.authenticated) { | |
browserHistory.push('/signin'); | |
} | |
} | |
componentWillUpdate(nextprops) { | |
if (!nextprops.authenticated) { | |
browserHistory.push('/signin'); | |
} | |
} | |
render() { | |
return ( | |
// {...this.props} ensures that each components props are not thrown away :p | |
<ComposedComponent {...this.props} /> | |
); | |
} | |
}; | |
return createContainer(() => { | |
return { authenticated: !!localStorage.getItem('auth') } | |
}, RequireAuth); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment