Skip to content

Instantly share code, notes, and snippets.

@themojilla
Created April 14, 2018 19:36
Show Gist options
  • Save themojilla/449804d4193b2f04f6777d8c3016b8ab to your computer and use it in GitHub Desktop.
Save themojilla/449804d4193b2f04f6777d8c3016b8ab to your computer and use it in GitHub Desktop.
HOC wrapper for react-router-config routes
import React from 'react';
import { connect } from 'react-redux';
import { Redirect } from 'react-router-dom';
export default ChildComponent => {
class RequireAuth extends React.PureComponent {
render() {
switch (this.props.isAuthenticated) {
case false:
return <Redirect to="/login" />;
default:
return <ChildComponent {...this.props} />;
}
}
}
const mapStateToProps = ({ auth }) => {
return {
isAuthenticated: auth.isAuthenticated
};
};
return connect(mapStateToProps)(RequireAuth);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment