Skip to content

Instantly share code, notes, and snippets.

@clairechabas
Last active August 6, 2019 11:04
Show Gist options
  • Save clairechabas/67c35be23a000d55a4d0271288c1f426 to your computer and use it in GitHub Desktop.
Save clairechabas/67c35be23a000d55a4d0271288c1f426 to your computer and use it in GitHub Desktop.
requireAuth : HOC to restrict access to components based on auth status
import React, { useEffect } from "react";
import { connect } from "react-redux";
export default ChildComponent => {
const ComposedComponent = props => {
useEffect(() => {
if (props.auth.isLoaded && props.auth.isEmpty) return props.history.push("/");
}, [props.auth, props.history]);
return <ChildComponent {...props} />;
};
function mapStateToProps(state) {
return {
auth: state.firebaseReducer.auth
};
}
return connect(mapStateToProps)(ComposedComponent);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment