In this shot, we’re going to discuss how we can integrate Google authentication in React Native For the Android version of the application.
Create a new React Native application from your terminal.
npx react-native init authtutorial
cd authtutorial
Go to the Firebase dashboard and create an Android and iOS application named com.authtutorial. We’ll get a file called googleservices.json
for Android. We will be needing the file for integrating Firebase services in our React Native application.
Place the googleservices.json
inside the android > app folder. Follow the below steps to initialize the Firebase in the Android version of the application.
classpath 'com.google.gms:google-services:4.3.10'
apply plugin: 'com.google.gms.google-services'
dependencies
in the same filedependencies {
// Import the Firebase BoM
implementation platform('com.google.firebase:firebase-bom:29.0.3')
npm install --save @react-native-firebase/app @react-native-firebase/auth
import React, {useState, useEffect} from 'react';import {SafeAreaView, Text, TouchableOpacity, View} from 'react-native';import auth from '@react-native-firebase/auth';import {GoogleSignin,GoogleSigninButton,} from '@react-native-google-signin/google-signin';GoogleSignin.configure({webClientId:'745305328860-3e60bmsvapbft8kh1dr0vf0u3h2239cp.apps.googleusercontent.com',});const App = () => {const [user, setUser] = useState(null);const onGoogleButtonPress = async () => {const {idToken} = await GoogleSignin.signIn();const googleCredential = auth.GoogleAuthProvider.credential(idToken);return auth().signInWithCredential(googleCredential);};const onAuthStateChanged = async userAuth => {if (!userAuth) {return;}if (userAuth) {console.log(userAuth);setUser(userAuth);}return () => userReference();};useEffect(() => {const subscriber = auth().onAuthStateChanged(onAuthStateChanged);return () => {subscriber;};}, []);const signOut = async () => {auth().signOut();setUser(null);return () => userReference();};return (<SafeAreaView style={{alignItems: 'center', flex: 1, marginTop: 100}}><View style={{margin: 10}}><Text>Google Sign In Tutorial</Text></View><View style={{margin: 10}}>{user === null && (<GoogleSigninButtonstyle={{width: 312, height: 48}}size={GoogleSigninButton.Size.Wide}color={GoogleSigninButton.Color.Light}onPress={onGoogleButtonPress}/>)}</View>{user !== null && (<View style={{margin: 10}}><Text style={{margin: 10}}>{user.displayName}</Text><TouchableOpacity onPress={signOut} style={{alignItems: 'center'}}><Text>Sign Out</Text></TouchableOpacity></View>)}</SafeAreaView>);};export default App;
Here, we import the necessary packages required for Google sign-in. The objective is to sign in the user with Google OAuth. So we include a Google “SignIn” button from @react-native-google-signin/google-signin
.
We create a function to sign in the user, and when the user is successfully signed in, we need to create a function that listens to the authChanges.
We also create a function that will enable the user to sign out from Google.
Now go to the terminal and run:
npm run android