Implementing Google Sign-In using Firebase with React Native.
1. Install react-native google-signin package
npm i @react-native-community/google-signin2. Firebase Configuration
Now, we need to create Firebase Project first. You can open the link here to open Firebase Console. After that, page like this will be displayed and just click create a project button.
Fill the requirements with your project name, or you can just use your app’s name.
Note:
- To find Android package name, you can find it from the file here (yourProject -> android -> app -> src -> main -> java -> yourPackageName -> yourAppName -> MainActivity.java). The word after package is your app package name.
Next, download
google-services.json, it will be required when we setup in Android folder later.Now open project setting from firebase, we will set the SHA-1 from here, click Project Overview then Project settings.
You can get your SHA-1 fingerprint just by follow this guide (first answer) here. If you don’t have keystore yet, you need to make one, follow this guide here to make it. Just comment if you’re confused about it, I will try to help.
3. Android Installation
A. Update the code in android/build.gradle with
buildscript { ext {
buildToolsVersion = "33.0.0"
minSdkVersion = 21
compileSdkVersion = 33
targetSdkVersion = 33 ndkVersion = "23.1.7779620" kotlinVersion = "1.7.0" googlePlayServicesAuthVersion = "16.0.1" // <--- use this version or newer
}
...
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2' // <--- use this version or newer
classpath 'com.google.gms:google-services:4.1.0' // <--- use this version or newer classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin: $kotlinVersion"
}
...
allprojects {
repositories {
mavenLocal()
google() // <--- make sure this is included
jcenter()
}
}
B. Update the code in android/app/build.gradle with
...
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.android.support:appcompat-v7:23.0.1"
implementation "com.facebook.react:react-native:+"
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0' // <-- add this; newer versions should work too
}
apply plugin: 'com.google.gms.google-services' // <--- this should be the last line
C. Copy google-services.json file (downloaded from Firebase configuration before) to yourProject -> android -> app. Complete guide here.
4. Setup Google Login Method
The configuration is already completed, from Firebase to Android folder in React Native Project. Now, it’s time to play around with the code. You can use the method below.
const GoogleLogin = async () => {
GoogleSignin.configure();
try {
await GoogleSignin.hasPlayServices();
const userInfo = await GoogleSignin.signIn();
} catch (error) {
console.log(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
}
}
};


A. DEVELOPER ERROR:
ReplyDeleteSolution -
1. You don’t copy google-services.json file correctly.
2. You register the wrong SHA-1 fingerprint in your Firebase Project. There’s two kind of SHA-1 fingerprint, one for testing and the other one for app. You might accidentally register the testing one, the solution is here. Follow the second answer and open it via Android Studio for the complete SHA-1 data.
3.If that still doesn’t work, add your app’s SHA-26 to Firebase Project.
4. Add keystore file and it's configuration properly.
project/android/gradle.properties
MYAPP_RELEASE_STORE_FILE=my-upload-key.keystore
MYAPP_RELEASE_KEY_ALIAS=my-key-alias
MYAPP_RELEASE_STORE_PASSWORD=password
MYAPP_RELEASE_KEY_PASSWORD=password
project/android/app/build.gradle
signingConfigs {
// debug {
// storeFile file('debug.keystore')
// storePassword 'android'
// keyAlias 'androiddebugkey'
// keyPassword 'android'
// }
debug {
storeFile file(MYAPP_RELEASE_STORE_FILE)
storePassword MYAPP_RELEASE_STORE_PASSWORD
keyAlias MYAPP_RELEASE_KEY_ALIAS
keyPassword MYAPP_RELEASE_KEY_PASSWORD
}
release {
if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
storeFile file(MYAPP_RELEASE_STORE_FILE)
storePassword MYAPP_RELEASE_STORE_PASSWORD
keyAlias MYAPP_RELEASE_KEY_ALIAS
keyPassword MYAPP_RELEASE_KEY_PASSWORD
}
}
}
B. SIGN IN — IN PROGRESS
ReplyDeleteIt could be DEVELOPER_ERROR in disguise. When you try attempt to login more than once, the second error will be SIGN IN — IN PROGRESS. In fact, the main problem is still DEVELOPER_ERROR.
You will get SIGN_IN-IN_PROGRESS message if the authenticate process is not over yet but you’re trying to sign in again.
C. For iOS
ReplyDelete1. Add GoogleService-Info.plist in ios/project
2. Configure URL types in the Info panel.
In Xcode, with project selected as target, go to Info tab and expand URL Types. Click ‘+’ button at the bottom of URL Types section and add a URL with scheme set to your REVERSED_CLIENT_ID which can be found inside GoogleService-Info.plist