-
Notifications
You must be signed in to change notification settings - Fork 61
DOCSP-4883 Document OAuth2 login with React Native #253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| c83441369227c697b1e9b4c824c41b5a | ||
| a4fa2fafc83bfa36b518faaac6ed9e6a |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| function example() {} function untested({View, Text, Button, React, GoogleSignin, Stitch, GoogleCredential, GoogleSigninButton}) { // too much boilerplate to be able to test this | ||
| // Example component that uses the react-native-google-signin module to get | ||
| // the server auth code for Stitch to use. | ||
| // | ||
| // NOTE: The react-native-google-signin native module must be installed correctly | ||
| // and your Google project must be configured. | ||
| // | ||
| // For more detail, see https://stackoverflow.com/questions/55145071/#55173164 | ||
| // | ||
| // Above: | ||
| // import {Stitch, GoogleCredential} from 'mongodb-stitch-react-native-sdk' | ||
| // import {GoogleSignin, GoogleSigninButton} from 'react-native-google-signin' | ||
| class GoogleLogin extends React.Component { | ||
| // ... other component methods ... | ||
|
|
||
| componentDidMount() { | ||
| // Configure react-native-google-signin | ||
| GoogleSignin.configure({ | ||
| webClientId: '<id>', // client ID of type WEB that is found in the Google project configuration | ||
| offlineAccess: true, // Must be `true` to allow the Stitch service to use credential | ||
| iosClientId: '<id>', // [iOS] CLIENT_ID from GoogleService-Info.plist | ||
| }) | ||
| } | ||
|
|
||
| _onPressLogin = async () => { | ||
| // It's recommended to call this before signIn() | ||
| await GoogleSignin.hasPlayServices() | ||
|
|
||
| // Sign in via react-native-google-signin | ||
| const userInfo = await GoogleSignin.signIn() | ||
|
|
||
| // Retrieve the server auth code | ||
| const {serverAuthCode} = userInfo | ||
|
|
||
| // serverAuthCode will be null if something went wrong | ||
| if (serverAuthCode === null) { | ||
| throw new Error('Failed to get serverAuthCode!') | ||
| } | ||
| try { | ||
| // Pass auth code to Stitch with a GoogleCredential | ||
| const {auth} = Stitch.defaultAppClient | ||
| const user = await auth.loginWithCredential(new GoogleCredential(serverAuthCode)) | ||
|
|
||
| // Log in was successful | ||
| console.log(`Successfully logged in as user ${user.id}`) | ||
| this.setState({currentUserId: user.id}) | ||
| } catch (err) { | ||
| // Login failed | ||
| console.error(`Failed to log in: ${err}`) | ||
| this.setState({currentUserId: undefined}) | ||
| } | ||
| } | ||
|
|
||
| _onPressLogout = async () => { | ||
| // Logout react-native-google-signin | ||
| await GoogleSignin.revokeAccess() | ||
| await GoogleSignin.signOut() | ||
|
|
||
| // Then log Stitch out | ||
| const {auth} = Stitch.defaultAppClient | ||
| const user = await auth.logout() | ||
|
|
||
| console.log(`Successfully logged out user ${user.id}`) | ||
| this.setState({currentUserId: undefined}) | ||
| } | ||
|
|
||
| render() { | ||
| let loginStatus = 'Currently logged out.' | ||
| const {currentUserId, isSigninInProgress} = this.state | ||
| if (currentUserId) { | ||
| loginStatus = `Currently logged in as ${currentUserId}.` | ||
| } | ||
|
|
||
| // Leverage react-native-google-signin's GoogleSigninButton | ||
| const loginButton = ( | ||
| <GoogleSigninButton | ||
| style={{width: 192, height: 48}} | ||
| size={GoogleSigninButton.Size.Wide} | ||
| color={GoogleSigninButton.Color.Dark} | ||
| onPress={this._onPressLogin} | ||
| disabled={isSigninInProgress} | ||
| /> | ||
| ) | ||
|
|
||
| const logoutButton = ( | ||
| <Button | ||
| onPress={this._onPressLogout} | ||
| title="Logout" | ||
| /> | ||
| ) | ||
|
|
||
| return ( | ||
| <View> | ||
| <Text> {loginStatus} </Text> | ||
| {currentUserId === undefined ? loginButton : logoutButton} | ||
| </View> | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| function example({AnonymousCredential, UserPasswordCredential, stitchAppClient}) { | ||
| // Previously: | ||
| // const stitchAppClient = await Stitch.initializeDefaultAppClient('your-stitch-app-id') | ||
|
|
||
| // Log in with anonymous credential | ||
| stitchAppClient.auth | ||
| .loginWithCredential(new AnonymousCredential()) | ||
| .then((user) => { | ||
| console.log(`Logged in as anonymous user with id: ${user.id}`) | ||
| }) | ||
| .catch(console.error) | ||
|
|
||
| // Log in with user/password credential | ||
| stitchAppClient.auth | ||
| .loginWithCredential(new UserPasswordCredential('user', 'password')) | ||
| .then((user) => { | ||
| console.log(`Logged in as user with id: ${user.id}`) | ||
| }) | ||
| .catch(console.error) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| function example() {} function untested({View, Text, Button, React, GoogleSignin, Stitch, GoogleCredential, GoogleSigninButton}) { // too much boilerplate to be able to test this | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get the reasoning for this change, but it seems just a bit incomplete to not include the anonymous and email/password (any maybe others) login snippets here too. Maybe later down the road we can look into some kind of tabbed experience in the SDK docs to pick the credential you want an example for so that we can include them here as well as in StitchAuth.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't worry, those snippets are still available elsewhere in the docs! |
||
| // Example component that uses the react-native-google-signin module to get | ||
| // the server auth code for Stitch to use. | ||
| // | ||
| // NOTE: The react-native-google-signin native module must be installed correctly | ||
| // and your Google project must be configured. | ||
| // | ||
| // For more detail, see https://stackoverflow.com/questions/55145071/#55173164 | ||
| // | ||
| // Above: | ||
| // import {Stitch, GoogleCredential} from 'mongodb-stitch-react-native-sdk' | ||
| // import {GoogleSignin, GoogleSigninButton} from 'react-native-google-signin' | ||
| class GoogleLogin extends React.Component { | ||
| // ... | ||
|
|
||
| componentDidMount() { | ||
| // Configure react-native-google-signin | ||
| GoogleSignin.configure({ | ||
| webClientId: '<id>', // client ID of type WEB from Stitch | ||
| offlineAccess: true, // allows Stitch service to use credential | ||
| iosClientId: '<id>', // [iOS] CLIENT_ID from GoogleService-Info.plist | ||
| }) | ||
| } | ||
|
|
||
| _onPressLogin = async () => { | ||
| // It's recommended to call this before signIn() | ||
| await GoogleSignin.hasPlayServices() | ||
|
|
||
| // Sign in via react-native-google-signin | ||
| const userInfo = await GoogleSignin.signIn() | ||
|
|
||
| // Retrieve the server auth code | ||
| const {serverAuthCode} = userInfo | ||
| if (serverAuthCode === null) { | ||
| throw new Error('Failed to get serverAuthCode!') | ||
| } | ||
| try { | ||
| // Pass auth code to Stitch with a GoogleCredential | ||
| const {auth} = Stitch.defaultAppClient | ||
| const user = await auth.loginWithCredential(new GoogleCredential(serverAuthCode)) | ||
|
|
||
| // Log in was successful | ||
| console.log(`Successfully logged in as user ${user.id}`) | ||
| this.setState({currentUserId: user.id}) | ||
| } catch (err) { | ||
| // Login failed | ||
| console.error(`Failed to log in: ${err}`) | ||
| this.setState({currentUserId: undefined}) | ||
| } | ||
| } | ||
|
|
||
| _onPressLogout = async () => { | ||
| // Logout react-native-google-signin | ||
| await GoogleSignin.revokeAccess() | ||
| await GoogleSignin.signOut() | ||
|
|
||
| // Then log Stitch out | ||
| const {auth} = Stitch.defaultAppClient | ||
| const user = await auth.logout() | ||
|
|
||
| console.log(`Successfully logged out user ${user.id}`) | ||
| this.setState({currentUserId: undefined}) | ||
| } | ||
|
|
||
| render() { | ||
| let loginStatus = 'Currently logged out.' | ||
| const {currentUserId, isSigninInProgress} = this.state | ||
| if (currentUserId) { | ||
| loginStatus = `Currently logged in as ${currentUserId}.` | ||
| } | ||
|
|
||
| // Leverage react-native-google-signin's GoogleSigninButton | ||
| const loginButton = ( | ||
| <GoogleSigninButton | ||
| style={{width: 192, height: 48}} | ||
| size={GoogleSigninButton.Size.Wide} | ||
| color={GoogleSigninButton.Color.Dark} | ||
| onPress={this._onPressLogin} | ||
| disabled={isSigninInProgress} | ||
| /> | ||
| ) | ||
|
|
||
| const logoutButton = ( | ||
| <Button | ||
| onPress={this._onPressLogout} | ||
| title="Logout" | ||
| /> | ||
| ) | ||
|
|
||
| return ( | ||
| <View> | ||
| <Text> {loginStatus} </Text> | ||
| {currentUserId === undefined ? loginButton : logoutButton} | ||
| </View> | ||
| ) | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does
serverAuthCode === nullimply that the login failed or was cancelled? Could add a comment clarifying the case that would cause the error.