Created
March 16, 2018 15:21
-
-
Save divyanshu013/54fd1d946fd369494d3e4094bde02362 to your computer and use it in GitHub Desktop.
RootComponent for todos native auth app
This file contains hidden or 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
... | |
// create auth0 service here | |
import Auth0 from 'react-native-auth0'; | |
const auth0 = new Auth0({ domain: 'divyanshu.auth0.com', clientId: '6ZR8Jgj6Gzy1onJhrO0egEbfudIBVZNP' }); | |
... | |
export default class RootComponent extends React.Component { | |
state = { | |
accessToken: null, | |
avatar: null, | |
name: null | |
} | |
// method to handle login | |
handleLogin = () => { | |
auth0 | |
.webAuth | |
.authorize({scope: 'openid profile email', audience: 'https://todosnative'}) | |
.then((credentials) => { | |
auth0 | |
.auth | |
.userInfo({token: credentials.accessToken}) | |
.then((user) => { | |
this.setState({ | |
accessToken: credentials.accessToken, | |
avatar: user.picture, | |
name: user.nickname | |
}); | |
}) | |
.catch(error => console.error(error)) | |
}) | |
.catch(error => console.error(error)); | |
} | |
// method to handle logout | |
handleLogout = () => { | |
this.setState({ | |
accessToken: null, | |
avatar: null, | |
name: null | |
}); | |
} | |
... | |
render = () => { | |
return ( | |
<ReactiveBase app={CONFIG.app} credentials={CONFIG.credentials} type={CONFIG.type}> | |
<Container> | |
<MainTabNavigator | |
// pass via props | |
screenProps={{ | |
accessToken: this.state.accessToken, | |
avatar: this.state.avatar, | |
name: this.state.name, | |
handleLogin: this.handleLogin, | |
handleLogout: this.handleLogout | |
}} | |
/> | |
</Container> | |
</ReactiveBase> | |
); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment