Created
October 27, 2016 15:09
-
-
Save boyboi86/ce165b6a27e5022bb2bef550c2cf16e1 to your computer and use it in GitHub Desktop.
higher order component for react to secure routes
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
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