Last active
August 6, 2019 11:04
-
-
Save clairechabas/67c35be23a000d55a4d0271288c1f426 to your computer and use it in GitHub Desktop.
requireAuth : HOC to restrict access to components based on auth status
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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