Skip to content

Instantly share code, notes, and snippets.

@boyboi86
Created October 27, 2016 15:09
Show Gist options
  • Save boyboi86/ce165b6a27e5022bb2bef550c2cf16e1 to your computer and use it in GitHub Desktop.
Save boyboi86/ce165b6a27e5022bb2bef550c2cf16e1 to your computer and use it in GitHub Desktop.
higher order component for react to secure routes
export default function (ComposedComponent) {
class Authentication extends Component{
/*Defined contextTypes to access context */
static contextTypes = {
router: React.PropTypes.object
}
/*Made used of context to define route*/
componentWillMount(){
if(!this.props.authenticated){
this.context.router.push("/");
}
}
/*If component update check if user still sign in */
componentWillUpdate(nextProps){
if(!nextProps.authenticated){
this.context.router.push("/");
}
}
render(){
return <ComposedComponent { ...this.props } />
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment