Technetium 101(React Native): Google Social Login



Version

  • react-native: 0.63.4
  • react-native-community/google-signin: 4.0.0

Step 1 : Install package

yarn add @react-native-community/google-signin

Step 2 : Create firebase project

  • Search for Firebase and go to Firebase Console
  • Create a project by clicking on Add Project.
  • After done create project, click the android icon in the firebase Console Dashboard
  • Fill in the following fields of "Add Firebase to your Android app" to generate config file ( i.e. google-services.json)
    • Android package name (check at android manifest or android/app/build.gradle)
    • Debug signing certificate SHA-1
      • open cmd
      • go to project directory and paste
      • keytool -list -v -keystore android/app/debug.keystore -alias androiddebugkey -storepass android -keypass android
    • Release signing certificate SHA-1
      • open cmd
      • go to jdk\bin directory and paste. eg `C:\Program Files\Java\jdk1.8.0_121\bin`
      • keytool -list -v -keystore {path_to_your_.jks_file} -alias {alias_name}


  • When you are done registering your android app with firebase then Download the Config file and place it in android/app
  • Add Firebase SDK in Project-level build.gradle i.e. android/build.gradle
  • Add Google Play Services plugin in App-level build.gradle (android/appp/build.gradle):
    • apply plugin: 'com.google.gms.google-services' // Add at end of the file
  • In firebase, you will need to enable Google option in SignIn Method section
  • While enabling Google, do copy the Web Client ID from there, we need this later on


Step 3 : Add code in react native project for Login

  • Import Public Api of @react-native-community/google-signin
import {
  GoogleSignin,
  GoogleSigninButton,
  statusCodes,
} from '@react-native-community/google-signin';
  • But before you can use above imports you need call once, configure of GoogleSignin. You can call it in ComponentDidMount life Cycle method or you can use useEffect Hook
GoogleSignin.configure({
 webClientId: WebClientID, // client ID of type WEB for your server(needed to verify user ID and offline access)
 offlineAccess: true, // if you want to access Google API on behalf of the user FROM YOUR SERVER
 forceCodeForRefreshToken: true, // [Android] related to `serverAuthCode`, read the docs link below *.
 accountName: '', // [Android] specifies an account name on the device that should be used
    });
  • Sign in function
 signIn = async () => {
    try {
      await GoogleSignin.hasPlayServices();
      const info = await GoogleSignin.signIn();
      console.warn({userInfo: info});
      setUserInfo(info);
    } catch (error) {
      if (error.code === statusCodes.SIGN_IN_CANCELLED) {
        // user cancelled the login flow
      } else if (error.code === statusCodes.IN_PROGRESS) {
        // operation (e.g. sign in) is in progress already
      } else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
        // play services not available or outdated
      } else {
        // some other error happened
      }
    }
  };
  • Sign out function
signOut = async () => {
    try {
      await GoogleSignin.revokeAccess();
      await GoogleSignin.signOut();
      setUserInfo(null); // Remember to remove the user from your app's state as well
    } catch (error) {
      console.error(error);
    }
  };
  • Button
<GoogleSigninButton
    style={{ width: 192, height: 48 }}
    size={GoogleSigninButton.Size.Wide}
    color={GoogleSigninButton.Color.Dark}
    onPress={this._signIn}
    disabled={this.state.isSigninInProgress} 
/>

Reference

SHA-1


Comments

Popular posts from this blog

Build App Insights: Mobile app development trends 2021

Build App Insights: Skeleton Screens Benefits

Technetium 101(React Native): Performance Optimization