Skip to content

Instantly share code, notes, and snippets.

@trinadhkoya
Last active December 25, 2018 16:45
Show Gist options
  • Save trinadhkoya/29fd97706552c4da32b7bcc8a702b354 to your computer and use it in GitHub Desktop.
Save trinadhkoya/29fd97706552c4da32b7bcc8a702b354 to your computer and use it in GitHub Desktop.
This gist is about checking the authentication on every component
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