Skip to content

Instantly share code, notes, and snippets.

@motephyr
Created March 13, 2016 15:14
Show Gist options
  • Save motephyr/c34fbfe058ea68dd2d70 to your computer and use it in GitHub Desktop.
Save motephyr/c34fbfe058ea68dd2d70 to your computer and use it in GitHub Desktop.
AuthenticateComponent
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