Created
February 1, 2020 10:16
Workout Tracker withAuthentication
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 from 'react'; | |
import AuthUserContext from './context'; | |
import { withFirebase } from '../Firebase'; | |
const withAuthentication = Component => { | |
class WithAuthentication extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
authUser: null, | |
}; | |
} | |
componentDidMount() { | |
this.listener = this.props.firebase.auth.onAuthStateChanged( | |
authUser => { | |
authUser | |
? this.setState({ authUser }) | |
: this.setState({ authUser: null }); | |
}, | |
); | |
} | |
componentWillUnmount() { | |
this.listener(); | |
} | |
render() { | |
return ( | |
<AuthUserContext.Provider value={this.state.authUser}> | |
<Component {...this.props} /> | |
</AuthUserContext.Provider> | |
); | |
} | |
} | |
return withFirebase(WithAuthentication); | |
}; | |
export default withAuthentication; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment