| 3 | import { updatePath } from 'redux-simple-router'; |
| 4 | |
| 5 | export function requireAuthentication(Component) { |
| 6 | class AuthenticateComponent extends React.Component { |
| 7 | |
| 8 | constructor(props) { |
| 9 | super(props); |
| 10 | } |
| 11 | |
| 12 | componentWillMount() { |
| 13 | const { isAuthenticated, dispatch } = this.props; |
| 14 | if (!isAuthenticated) { |
| 15 | dispatch(updatePath(`/login?next=${this.props.location.pathname}`)); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | render() { |
| 20 | return ( |
| 21 | <Component {...this.props} /> |
| 22 | ); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | AuthenticateComponent.propTypes = { |
| 27 | dispatch: PropTypes.func.isRequired, |
| 28 | isAuthenticated: PropTypes.bool, |
| 29 | location: PropTypes.object |
| 30 | }; |
| 31 | |
| 32 | function mapStateToProps(state) { |
| 33 | return { |
| 34 | token: state.user.token, |
| 35 | isAuthenticated: state.user.authenticated |
| 36 | }; |
| 37 | } |
| 38 | |
| 39 | return connect(mapStateToProps)(AuthenticateComponent); |
| 40 | }; |