In today’s mobile landscape, providing seamless user login options is crucial for app adoption
and user retention. React Native, with its cross-platform capabilities, empowers you to build
beautiful apps, but handling login across different platforms can be a challenge. Enter social
logins like Google, Facebook, and Apple Sign-in, offering a convenient and familiar way for
users to sign up or log in to your React Native app.
This blog post will guide you through implementing these popular social login options in your
React Native project, complete with code examples.
SetUpGoogleDeveloper Console: Create a project in the Google Developer Console
(https://developers.google.com/workspace/guides/create-project) and enable the Google
Sign-in API.
Install Library: Install the @react-native-google-signin/google-signin library using npm or
yarn
In terminal
npm install @react-native-google-signin/google-signin
Configure Google Sign-in: Configure Google Sign-in with your OAuth client ID obtained
from the Google Developer Console. You’ll need separate client IDs for Android and iOS (if
applicable). Here’s an example:
import { GoogleSignin } from
'@react-native-google-signin/google-signin';
GoogleSignin.configure({
webClientId: 'YOUR_WEB_CLIENT_ID',
androidClientId: 'YOUR_ANDROID_CLIENT_ID',
iosClientId: 'YOUR_IOS_CLIENT_ID',
scopes: ['profile', 'email'], // Request access to profile and email
data
});
Sign-in Flow: Implement a function to handle Google Sign-in using GoogleSignin.signIn().
This returns a Google Sign-in user object containing user information like ID, name, and
email.
const handleGoogleSignIn = async () => {
try {
const userInfo = await GoogleSignin.signIn();
// Access user information from userInfo object
console.log('User signed in with Google:', userInfo);
} catch (error) {
console.error('Error signing in with Google:', error);
}
};
SetUpFacebookDeveloper Account: Create a Facebook developer account
(https://developers.facebook.com/) and create a new app.
Install Library: Install the react-native-fbsdk library using npm or yarn:
In terminal
npm install react-native-fbsdk
Configure Facebook Login: Configure the library with your Facebook App ID obtained from
the developer account. You’ll also need to configure Android and iOS settings following the
library’s documentation.
Sign-in Flow: Utilize LoginManager.logInWithPermissions to initiate Facebook login.
Request the necessary permissions (e.g., public_profile, email) and handle the access token
to retrieve user information
import {
LoginManager,
AccessToken
}
from 'react-native-fbsdk';
const handleFacebookSignIn = async() = >{
try {
const loginResult = await LoginManager.logInWithPermissions(['public_profile', 'email']);
if (!loginResult.isCancelled) {
const accessToken = await AccessToken.getCurrentAccessToken();
// Access user information using the access token
console.log('User signed in with Facebook:', accessToken);
}
} catch(error) {
console.error('Error signing in with Facebook:', error);
}
};
SetUpAppleDeveloper Account: You’ll need an Apple Developer account with a valid
Apple Developer Program membership to enable Apple Sign-in.
EnableApple Sign-in: In your Xcode project capabilities, enable the Sign In with Apple
capability.
Install Library: While Apple Sign-in is a built-in framework, consider using a library like
react-native-apple-authentication for a more streamlined experience. Install it using npm or
yarn
In terminal
npm install react-native-apple-authentication
Sign-in Flow: Utilize the library’s functions like performRequest to initiate Apple Sign-in.
Specify the requested operation (AppleAuthRequestOperation.LOGIN) and scopes
(AppleAuthRequestScope.EMAIL, AppleAuthRequestScope.FULL_NAME) to request user
information. The library handles the native iOS interactions and returns the user information
through the response object.
import * as AppleAuth from 'react-native-apple-authentication';
const handleAppleSignIn = async () => {
try {
const appleAuthRequestResponse = await AppleAuth.performRequest({
requestedOperation: AppleAuth.Operation.LOGIN,
requestedScopes: [AppleAuth.Scope.EMAIL,
AppleAuth.Scope.FULL_NAME],
});
const { user, identityToken } = appleAuthRequestResponse;
// Access user information like user ID, name, and email (if
requested)
console.log('User signed in with Apple:', user, identityToken);
} catch (error) {
console.error('Error signing in with Apple:', error);
}
};
Important Note: Apple Sign-in requires an iOS device for testing and development. You can
use TestFlight or a physical device to test this functionality.
Remember: Always refer to the official documentation for each library and platform for the latest
updates and detailed configuration instructions.
By integrating Google, Facebook, and Apple Sign-in (for iOS) into your React Native app, you
can significantly enhance the user login experience, improve sign-up rates, and potentially
enrich your user profiles with valuable data. With these powerful tools at your disposal, you can
create a more user-friendly and engaging app for your React Native audience