Last active
December 22, 2018 05:52
-
-
Save trinadhkoya/d482820c090be34ecda909337ea5baae to your computer and use it in GitHub Desktop.
An HOC which checks authentication in every container/screen
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, {Component} from 'react' | |
import {connect} from 'react-redux' | |
export default function (ComposedComponent) { | |
class Authentication extends Component { | |
componentWillMount() { | |
if (!this.props.authenticated) this.props.navigation.navigate('LoginForm') | |
} | |
componentWillUpdate(nextProps) { | |
if (!nextProps.authenticated) this.props.navigation.navigate('LoginForm') | |
} | |
render() { | |
return ( | |
<ComposedComponent {...this.props} /> | |
) | |
} | |
} | |
const mapStateToProps = ({auth}) => { | |
return { | |
authenticated: auth.isAuthenticated | |
} | |
} | |
return connect(mapStateToProps)(Authentication) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment