The Google OAuth2 authentication code contained within this credential.
The contents of this credential as they will be passed to the Stitch server.
The behavior of this credential when logging in.
The name of the provider for this credential.
The type of the provider for this credential.
Generated using TypeDoc
GoogleCredential
A credential which can be used to log in as a Stitch user using the Google authentication provider.
Browser SDK users can use the GoogleRedirectCredential with StitchAuth.loginWithRedirect. Server and React Native SDK users must obtain their own server auth code. Use a third-party module to get this code and pass it to the GoogleCredential constructor.
Example
// 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> ) } }