Skip to content

Instantly share code, notes, and snippets.

@nesbtesh
Last active February 24, 2017 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nesbtesh/34862ee9cc6aeb91091089d0e796d5a3 to your computer and use it in GitHub Desktop.
Save nesbtesh/34862ee9cc6aeb91091089d0e796d5a3 to your computer and use it in GitHub Desktop.
import React from "react";
import {connect} from "react-redux";
import {bindActionCreators} from "redux";
import { push, replace } from "react-router-redux";
class AuthContainer extends React.Component {
componentWillReceiveProps(nextProps) {
this.isAuthenticated();
}
componentWillMount(){
this.isAuthenticated();
}
isAuthenticated = () => {
//if you also have expiration date you can also check if it has experied
//var now = new Date();
//if(this.props.auth.expiration <= now || !this.props.auth.isAuthenticated){
if(!this.props.auth.isAuthenticated){
this.props.actions.replace("/");
}
}
render() {
const {props} = this;
return (
<div>
{
React.Children.map(this.props.children, function(child) {
return React.cloneElement(
child,
{ ...props
}
);
})
}
</div>
);
}
}
const mapStateToProps = (state) => {
return state;
};
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(
{
push,
replace,
...Actions,
}, dispatch)
};
}
export default connect (
mapStateToProps,
mapDispatchToProps
)(AuthContainer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment