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