Description
In this article we will learn how to implement Google Login in React Native. I will explain this step by step. I assume that you have basic knowledge of React js. So let’s start our implementation.
Output
Prerequisite
To create fa Futter app follow the following steps,
Step 1
First step is to create a new React Native app. Following is the command for that:
- npx react-native init GPlusLogin
- cd GPlusLogin
Step 2
Then, run the app. I am running the project on my device using the following command:
So now we are having a default app run on our device.
Step 3
Now, we will install a plugin to implement Google login in React native app. Following is the plugin installation command for that:
- npm i react-native-google-signin
Step 4
Step 5
Once Android setup is done by following Step 4, you have google-service.json file. Place the generated configuration file (google-services.json) into <YOUR_PROJECT_ROOT>/android/app
Step 6
Update android/build.gradle with
- buildscript {
- ...
- dependencies {
- ...
- classpath('com.google.gms:google-services:4.3.3')
- }
- ...
- }
Step 7
Update android/app/build.gradle with
- android {
- ...
- signingConfigs {
- release {
- storeFile file('debug.keystore')
- storePassword 'android'
- keyAlias 'androiddebugkey'
- keyPassword 'android'
- }
- }
- }
- dependencies {
- ...
- }
- apply plugin: 'com.android.application'
- apply plugin: 'com.google.gms.google-services’
Step 8
Great -- you are done with setup. Now we will programmatically implement Google login, silent login and logout. So below is the programming for that. Open App.js and place it into it.
- import React, { useEffect, useState } from 'react';
- import {
- StyleSheet,
- View,
- StatusBar,
- TouchableOpacity,
- Text,
- Image
- } from 'react-native';
- import { GoogleSignin, GoogleSigninButton, statusCodes } from 'react-native-google-signin';
- GoogleSignin.configure({
- webClientId: '--------------.apps.googleusercontent.com',
- });
- const App: () => React$Node = () => {
- const [isLoggedIn, setIsLoggedIn] = useState(false)
- const [userInfo, setUserInfo] = useState(null)
- useEffect(() => {
- getCurrentUserInfo();
- }, []);
- const getCurrentUserInfo = async () => {
- try {
- const userInfo = await GoogleSignin.signInSilently();
- console.log(userInfo);
- setIsLoggedIn(true);
- setUserInfo(userInfo);
- } catch (error) {
- if (error.code === statusCodes.SIGN_IN_REQUIRED) {
-
- } else {
-
- }
- }
- };
-
- const _signIn = async () => {
- try {
- await GoogleSignin.hasPlayServices();
- const userInfo = await GoogleSignin.signIn();
- console.log(userInfo);
- setIsLoggedIn(true);
- setUserInfo(userInfo);
- } catch (error) {
- console.log(error);
- if (error.code === statusCodes.SIGN_IN_CANCELLED) {
-
- } else if (error.code === statusCodes.IN_PROGRESS) {
-
- } else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
-
- } else {
-
- }
- }
- };
-
- const _signOut = async () => {
- try {
- await GoogleSignin.revokeAccess();
- await GoogleSignin.signOut();
- setIsLoggedIn(false);
- } catch (error) {
- console.error(error);
- }
- }
- return (
- <>
- <StatusBar barStyle="dark-content" />
- <View style={styles.container} >
- {
- !isLoggedIn ?
- <GoogleSigninButton
- style={{ width: 192, height: 48 }}
- size={GoogleSigninButton.Size.Wide}
- color={GoogleSigninButton.Color.Dark}
- onPress={_signIn}
- /> :
- <>
- <Text>Email: {userInfo ? userInfo.user.email : ""}</Text>
- <Text>Name: {userInfo ? userInfo.user.name : ""}</Text>
- <TouchableOpacity style={styles.signOutBtn} onPress={_signOut}>
- <Text style={styles.signOutBtnText}>Signout</Text>
- </TouchableOpacity>
- </>
- }
- </View>
- </>
- );
- };
-
- const styles = StyleSheet.create({
- container: { flex: 1, justifyContent: "center", alignItems: "center" },
- signOutBtn: {
- backgroundColor: "#556688",
- padding: 10,
- borderRadius: 10
- },
- signOutBtnText: {
- color: "white"
- }
- });
-
- export default App;
Step 9
Great you are done with it. Now run and check the app.
Conclusion
In this article we have learned how to integrate Google Login using React Native.