Skip to content

Instantly share code, notes, and snippets.

@Mozartted
Created May 25, 2018 10:05
Show Gist options
  • Save Mozartted/20951b33ca754b135ef5a44cee40bcad to your computer and use it in GitHub Desktop.
Save Mozartted/20951b33ca754b135ef5a44cee40bcad to your computer and use it in GitHub Desktop.
import React from "react";
import { connect } from "react-redux";
import { withRouter } from "react-router";
// import actions from "../Store/Actions";
class EnsureVisitorOnlyContainer extends React.Component {
componentDidMount() {
const { currentURL } = this.props;
var visitorRoutes = ["/", "", "terms", "conditions"];
var check = visitorRoutes.indexOf(currentURL) > -1;
if (this.props.isLoggedIn) {
// set the current url/path for future redirection (we use a Redux action)
// then redirect (we use a React Router method)
// dispatch(actions.setRedirectUrl(currentURL))
if (check) {
this.props.history.replace("/home");
}
}
}
render() {
if (!this.props.isLoggedIn) {
return this.props.children;
} else {
return null;
}
}
}
// Grab a reference to the current URL. If this is a web app and you are
// using React Router, you can use `ownProps` to find the URL. Other
// platforms (Native) or routing libraries have similar ways to find
// the current position in the app.
function mapStateToProps(state, ownProps) {
console.log(ownProps);
return {
isLoggedIn: state.isAuthenticated,
currentURL: ownProps.location.pathname
};
}
export default withRouter(connect(mapStateToProps)(EnsureVisitorOnlyContainer));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment