Last active
January 14, 2019 13:16
-
-
Save jarodsmk/bb9925c9901530376cbcc9ec6322c174 to your computer and use it in GitHub Desktop.
Oauth react snippet, taken from: https://robertovg.com/blog/o-auth-2-with-react-native-keeping-expo/
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
import React, { Component } from 'react'; | |
import { SF_OAUTH_URL, REMOTE_ACCESS_CONSUMER_KEY } from 'react-native-dotenv'; | |
import { AsyncStorage, Button, View, Text } from 'react-native'; | |
import { AuthSession } from 'expo'; | |
import { globalStyles } from '../constants/Styles'; | |
import ScreenKeys from '../constants/ScreenKeys'; | |
export default class SignInScreen extends Component { | |
static navigationOptions = { | |
title: 'Please sign in', | |
}; | |
state = { | |
errorCode: null, | |
}; | |
_signInAsync = async () => { | |
const { navigation } = this.props; | |
const redirectUrl = AuthSession.getRedirectUrl(); | |
const result = await AuthSession.startAsync({ | |
authUrl: | |
`${SF_OAUTH_URL}` + | |
`?response_type=token` + | |
`&client_id=${REMOTE_ACCESS_CONSUMER_KEY}` + | |
`&prompt=login` + | |
`&redirect_uri=${encodeURIComponent(redirectUrl)}`, | |
}); | |
console.log(result); | |
const { type, errorCode = 'You cancel or dismissed the login' } = result; | |
if (type === 'success') { | |
// Just simple way to store the token in this examples | |
await AsyncStorage.setItem('userToken', JSON.stringify(result)); | |
navigation.navigate(ScreenKeys.main); | |
} else { | |
/** | |
* Result types can be: cancel, dismissed or error (with errorCode) | |
*/ | |
this.setState({ errorCode }); | |
} | |
}; | |
render() { | |
const { errorCode } = this.state; | |
return ( | |
<View style={globalStyles.container}> | |
<Button title="Sign in!" onPress={this._signInAsync} /> | |
{errorCode ? <Text>{errorCode}</Text> : null} | |
</View> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment