Skip to content

Instantly share code, notes, and snippets.

@divyanshu013
Created March 16, 2018 15:21
Show Gist options
  • Save divyanshu013/54fd1d946fd369494d3e4094bde02362 to your computer and use it in GitHub Desktop.
Save divyanshu013/54fd1d946fd369494d3e4094bde02362 to your computer and use it in GitHub Desktop.
RootComponent for todos native auth app
...
// 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