Last active
February 24, 2017 20:11
-
-
Save nesbtesh/34862ee9cc6aeb91091089d0e796d5a3 to your computer and use it in GitHub Desktop.
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 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