Initially forked from react-native-google-signin
Project aims to provide react-native implementation for latest Google Sign-in SDK's used in Android and iOS with exception of no native sign-in button component.
Project is actively used in our own apps and maintained.
1. Creating google project
To work with Google's oAuth you will need to create your project via google cloud console or firebase.
After this step you will have your GoogleService-Info.plist
(iOS) and google-services.json
(Android) files that you need to add to your codebase.
If you have difficulties with google project creation, please do some online research or submit Stack Overflow question.
2. Install NPM module
npm install rn-google-sign-in
or yarn add rn-google-sign-in
3. iOS Setup
Supports projects with min target sdk set to ios 10
-
Install cocoapods (if you don't have them already)
-
Inside of your projects
/ios
directory runpod init
-
Step above will generate a
Podfile
, open it and addpod 'GoogleSignIn'
as a dependency -
From terminal run
pod install
-
Open your project in XCode using
.xworkspace
file, not.xcodeproj
-
Right click on your projects
Libraries
folder -> "Add files to ..." findnode_modules/rn-google-sign-in/ios/RNGoogleSignIn.xcodeproj
and add it. -
In XCode under your targets Build Phases expand "Link Binary With Libraries" section and add
libRNGoogleSignIn.a
-
In XCode under your targets Build Settings search for "Header Search Paths" and add
$(SRCROOT)/../node_modules/rn-google-sign-in/ios/RNGoogleSignIn
-
In XCode under your targets Info expand "Url Types", create new one and in URL Schemes section add whatever the value of
REVERSED_CLIENT_ID
is inside of yourGoogleService-Info.plist
file you got from google -
Add following the top of your projects
AppDelegate.m
file
#import "RNGoogleSignIn.h"
- Finnaly add this before
@end
statement in the bottom of yourAppDelegate.m
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<NSString *, id> *)options { // Handle Google Deep Links BOOL googleLink = [RNGoogleSignIn application:application openURL:url sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey] annotation:options[UIApplicationOpenURLOptionsAnnotationKey]]; return googleLink;}
4. Android Setup
NOTE: Your simulator needs to have google play services installed to work with google oAuth, you will also need to make sure your development signing is implemented correctly.
- In your projects
android/settings.gradle
add
include ':rn-google-sign-in', ':app'project(':rn-google-sign-in').projectDir = new File(rootProject.projectDir, '../node_modules/rn-google-sign-in/android')
- In your projects
android/build.gradle
add
dependencies { ... classpath 'com.google.gms:google-services:3.0.0' // <--- add thxsis}
- In your projects
android/app/build.gradle
add
dependencies { ... compile(project(":rn-google-sign-in")){ exclude group: "com.google.android.gms" } compile 'com.google.android.gms:play-services-auth:11.8.0'} apply plugin: 'com.google.gms.google-services'
- Inside your
MainApplication.java
add
// <-- add
5. Usage
;
configure (REQUIRED)
Mandotary method that needs to be called before starting work with the library, configures google sdk data, returns promise once done.
GoogleSignin;
hasPlayServices
Check if device has google play services installed. Always returns true on iOS.
With autoResolve
the library will prompt the user to take action to solve the issue.
try await GoogleSignin; // Play services are avaliable catch e console;
currentUserAsync
Asynchroneusly retrieves user data if there was previous sign in, null otherwise
const user = await GoogleSignin;console;
currentUser
Access user data
GoogleSignin;
signIn
Prompt modal to let user sign in in on his/her device
try const user = await GoogleSignin; console; catch e console;
getAccessToken (Android)
Obtain users access token
try const token = GoogleSignin; console; catch e console;
signOut
Sign user out from device
try await GoogleSignin; catch e console;
revokeAccess
Remove your application from users authorized applications
try await GoogleSignin; catch e consolel