← Back to portfolio

Implementing Apple & Google Sign-In in React Native

July 12, 20269 min read
React NativeAuthenticationSecurityMobile

Social sign-in is essential for minimizing user friction during authentication. In the React Native ecosystem, integrating Google and Apple authentication requires combining official libraries with native configuration. Let's look at how to implement both workflows cleanly in TypeScript alongside Xcode and Android credential setup.

1. Google Sign-In Native Setup & Code

First, install `@react-native-google-signin/google-signin`. On Android, add the Google Services plugin to your build files:

groovy
// android/build.gradle
dependencies {
    classpath 'com.google.gms:google-services:4.4.1'
}

// android/app/build.gradle
apply plugin: 'com.google.gms.google-services'

Generate a SHA-1 fingerprint for your keystore and add it to your Firebase/Google developer console. On iOS, you must add the reversed client ID as a URL Scheme in your Info.plist:

xml
<!-- ios/AppName/Info.plist -->
<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>com.googleusercontent.apps.YOUR_REVERSED_CLIENT_ID</string>
    </array>
  </dict>
</array>

Initialize Google SDK inside your React Native application using your Web Client ID:

typescript
import React, { useEffect } from 'react';
import { Button, Alert } from 'react-native';
import { GoogleSignin, statusCodes } from '@react-native-google-signin/google-signin';

export default function GoogleAuthButton() {
  useEffect(() => {
    GoogleSignin.configure({
      webClientId: 'YOUR_GOOGLE_WEB_CLIENT_ID.apps.googleusercontent.com',
      offlineAccess: true,
    });
  }, []);

  const handleGoogleSignIn = async () => {
    try {
      await GoogleSignin.hasPlayServices();
      const userInfo = await GoogleSignin.signIn();
      console.log('ID Token:', userInfo.idToken);
      Alert.alert('Success', `Welcome, ${userInfo.user.name}`);
    } catch (error: any) {
      if (error.code === statusCodes.SIGN_IN_CANCELLED) {
        console.log('Sign-in cancelled');
      } else {
        Alert.alert('Error', error.message);
      }
    }
  };

  return <Button title="Sign in with Google" onPress={handleGoogleSignIn} />;
}

2. Apple Sign-In Native Setup & Code

To support Apple sign-in, install `@react-native-apple-authentication/apple-authentication`. In Xcode, open the 'Signing & Capabilities' tab and click '+ Capability' to add 'Sign In with Apple'. This registers the entitlement with your Apple developer profile.

No Info.plist modifications are strictly required for basic Apple authentication, but you must ensure your provisioning profile supports the capability. Now, write the login component details:

typescript
import React from 'react';
import { View, StyleSheet } from 'react-native';
import { appleAuth, AppleButton } from '@react-native-apple-authentication/apple-authentication';

export default function AppleAuthButton() {
  const handleAppleSignIn = async () => {
    try {
      const appleAuthRequestResponse = await appleAuth.performRequest({
        requestedOperation: appleAuth.Operation.LOGIN,
        requestedScopes: [appleAuth.Scope.EMAIL, appleAuth.Scope.FULL_NAME],
      });

      const { identityToken, user } = appleAuthRequestResponse;
      if (identityToken) {
        console.log('Apple identity token:', identityToken);
        // Send token to your backend validation server
      }
    } catch (error: any) {
      console.log('Apple sign-in failed', error);
    }
  };

  return (
    <View style={styles.container}>
      <AppleButton
        buttonStyle={AppleButton.Style.BLACK}
        buttonType={AppleButton.Type.SIGN_IN}
        style={styles.appleBtn}
        onPress={handleAppleSignIn}
      />
    </View>
  );
}

const styles = StyleSheet.create({
  container: { padding: 16 },
  appleBtn: { width: '100%', height: 48 },
});