rn-google-sign-in v1.0.10
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
/iosdirectory runpod initStep above will generate a
Podfile, open it and addpod 'GoogleSignIn'as a dependencyFrom terminal run
pod installOpen your project in XCode using
.xworkspacefile, not.xcodeprojRight click on your projects
Librariesfolder -> "Add files to ..." findnode_modules/rn-google-sign-in/ios/RNGoogleSignIn.xcodeprojand add it.In XCode under your targets Build Phases expand "Link Binary With Libraries" section and add
libRNGoogleSignIn.aIn XCode under your targets Build Settings search for "Header Search Paths" and add
$(SRCROOT)/../node_modules/rn-google-sign-in/ios/RNGoogleSignInIn XCode under your targets Info expand "Url Types", create new one and in URL Schemes section add whatever the value of
REVERSED_CLIENT_IDis inside of yourGoogleService-Info.plistfile you got from googleAdd following the top of your projects
AppDelegate.mfile
#import "RNGoogleSignIn.h"- Finnaly add this before
@endstatement 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.gradleadd
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.gradleadd
dependencies {
...
classpath 'com.google.gms:google-services:3.0.0' // <--- add thxsis
}- In your projects
android/app/build.gradleadd
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.javaadd
import com.asimetriq.googlesignin.RNGoogleSignInPackage; // <-- add
public class MainApplication extends Application implements ReactApplication {
// ......
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new RNGoogleSignInPackage() // <-- add
);
}
// ......
}5. Usage
import { GoogleSignin } from "rn-google-sign-in";configure (REQUIRED)
Mandotary method that needs to be called before starting work with the library, configures google sdk data, returns promise once done.
GoogleSignin.configure({
iosClientId: "", // [iOS] Required avaliable in developer console or GoogleService-Info.plist
webClientId: "", // client ID of type WEB for your server (needed to verify user ID and offline access)
offlineAccess: true, // if you want to access Google API on behalf of the user FROM YOUR SERVER
hostedDomain: "", // specifies a hosted domain restriction
forceConsentPrompt: true, // [Android] if you want to show the authorization prompt at each login
accountName: "" // [Android] specifies an account name on the device that should be used
});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.hasPlayServices({ autoResolve: true });
// Play services are avaliable
} catch (e) {
console.error(e);
}currentUserAsync
Asynchroneusly retrieves user data if there was previous sign in, null otherwise
const user = await GoogleSignin.currentUserAsync();
console.log(user);currentUser
Access user data
GoogleSignin.currentUser();signIn
Prompt modal to let user sign in in on his/her device
try {
const user = await GoogleSignin.signIn();
console.log(user);
} catch (e) {
console.log(e);
}getAccessToken (Android)
Obtain users access token
try {
const token = GoogleSignin.getAccessToken();
console.log(token);
} catch (e) {
console.error(e);
}signOut
Sign user out from device
try {
await GoogleSignin.signOut();
} catch (e) {
console.error(e);
}revokeAccess
Remove your application from users authorized applications
try {
await GoogleSignin.revokeAccess();
} catch (e) {
console.error(e)l
}8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago