Implement Google+ Sign-In with React Native

前端 未结 3 1215
北荒
北荒 2021-02-13 15:16

I\'m wanting to integrate G+ Sign In (as per https://developers.google.com/+/mobile/ios/sign-in) in a React Native app. I have Facebook Sign In working via http://brentvatne.ca/

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-13 16:14

    EDIT 2017

    Within the Expo framework, which is now the default for react-native apps, there is built in Google Authentication available:

    Expo documentation: https://docs.expo.io/versions/latest/sdk/google.html

    Get Android and iOS client ids: https://console.developers.google.com/apis/credentials

    import React from 'react'
    import Expo from 'expo'
    import Button from 'react-native-button'
    
    class Login extends React.Component {
      signInWithGoogleAsync = async () => {
        try {
          const result = await Expo.Google.logInAsync({
            androidClientId: process.env.GOOGLE_ANDROID_CLIENT_ID,
            iosClientId: process.env.GOOGLE_IOS_CLIENT_ID,
            scopes: ['profile'],
          })
    
          if (result.type === 'success') {
            return result
          }
          return { cancelled: true }
        } catch (e) {
          return { error: e }
        }
      }
    
    
      onLoginPress = async () => {
        const result = await this.signInWithGoogleAsync()
        // if there is no result.error or result.cancelled, the user is logged in
        // do something with the result
      }
    
      render() {
        return ()
      }
    }
    

    OLD ANSWER

    There is now a library for signing in with Google+ for react-native: https://github.com/devfd/react-native-google-signin

提交回复
热议问题