Skip to content

Instantly share code, notes, and snippets.

@trinadhkoya
Last active December 22, 2018 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trinadhkoya/d482820c090be34ecda909337ea5baae to your computer and use it in GitHub Desktop.
Save trinadhkoya/d482820c090be34ecda909337ea5baae to your computer and use it in GitHub Desktop.
An HOC which checks authentication in every container/screen
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