-
-
Save motephyr/c34fbfe058ea68dd2d70 to your computer and use it in GitHub Desktop.
AuthenticateComponent
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, { PropTypes } from 'react'; | |
import { connect } from 'react-redux'; | |
import { pushPath } from 'redux-simple-router'; | |
export function requireAuthentication(Component) { | |
class AuthenticateComponent extends React.Component { | |
constructor(props) { | |
super(props); | |
} | |
componentWillMount() { | |
const { isAuthenticated, dispatch } = this.props; | |
if (!isAuthenticated) { | |
dispatch(pushPath(`/login?next=${this.props.location.pathname}`)); | |
} | |
} | |
render() { | |
return ( | |
<Component {...this.props} /> | |
); | |
} | |
} | |
AuthenticateComponent.propTypes = { | |
dispatch: PropTypes.func.isRequired, | |
isAuthenticated: PropTypes.bool, | |
location: PropTypes.object | |
}; | |
function mapStateToProps(state) { | |
return { | |
token: state.user.token, | |
isAuthenticated: state.user.authenticated | |
}; | |
} | |
return connect(mapStateToProps)(AuthenticateComponent); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment