Options
All
  • Public
  • Public/Protected
  • All
Menu

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>
    )
  }
}

Hierarchy

  • GoogleCredential

Implements

Index

Constructors

constructor

  • new GoogleCredential(authCode: string, providerName?: string): GoogleCredential

Properties

Private authCode

authCode: string

The Google OAuth2 authentication code contained within this credential.

material

material: object

The contents of this credential as they will be passed to the Stitch server.

Type declaration

  • [key: string]: string

providerCapabilities

providerCapabilities: ProviderCapabilities = new ProviderCapabilities(false)

The behavior of this credential when logging in.

providerName

providerName: string

The name of the provider for this credential.

providerType

providerType: string = GoogleAuthProvider.TYPE

The type of the provider for this credential.

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc