1.0.10 • Published 6 years ago

rn-google-sign-in v1.0.10

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

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

  1. Install cocoapods (if you don't have them already)

  2. Inside of your projects /ios directory run pod init

  3. Step above will generate a Podfile, open it and add pod 'GoogleSignIn' as a dependency

  4. From terminal run pod install

  5. Open your project in XCode using .xworkspace file, not .xcodeproj

  6. Right click on your projects Libraries folder -> "Add files to ..." find node_modules/rn-google-sign-in/ios/RNGoogleSignIn.xcodeproj and add it.

  7. In XCode under your targets Build Phases expand "Link Binary With Libraries" section and add libRNGoogleSignIn.a

  8. In XCode under your targets Build Settings search for "Header Search Paths" and add $(SRCROOT)/../node_modules/rn-google-sign-in/ios/RNGoogleSignIn

  9. 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 your GoogleService-Info.plistfile you got from google

  10. Add following the top of your projects AppDelegate.m file

#import "RNGoogleSignIn.h"
  1. Finnaly add this before @end statement in the bottom of your AppDelegate.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.

  1. 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')
  1. In your projects android/build.gradle add
dependencies {
    ...
    classpath 'com.google.gms:google-services:3.0.0' // <--- add thxsis
}
  1. 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'
  1. Inside your MainApplication.java add
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
}
1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0-alpha.9

6 years ago

1.0.0-alpha.8

6 years ago

1.0.0-alpha.7

6 years ago

1.0.0-alpha.6

6 years ago

1.0.0-alpha.4

6 years ago

1.0.0-alpha.3

6 years ago

1.0.0-alpha.2

6 years ago

1.0.0

6 years ago