1.0.0 • Published 6 years ago

empite-reactnative-googleauth v1.0.0

Weekly downloads
1
License
ISC
Repository
github
Last release
6 years ago

empite-reactnative-googleauth

Installation

NPM

npm install empite-reactnative-googleauth --save

Yarn

yarn add empite-reactnative-googleauth

To link with project

react-native link empite-reactnative-googleauth

Project setup and initialization

See Android guide and iOS guide

Public API

1. GoogleSignin

import { googleAuth } from 'empite-reactnative-googleauth';

googleAuth.configure({
  iosClientId: '<FROM DEVELOPER CONSOLE>', // only for iOS
})

Example to access Google Drive both from the mobile application and from the backend server

GoogleSignin.configure({
  scopes: ['https://www.googleapis.com/auth/drive.readonly'], // what API you want to access on behalf of the user, default is email and profile
  iosClientId: '<FROM DEVELOPER CONSOLE>', // only for iOS
  webClientId: '<FROM DEVELOPER CONSOLE>', // 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
})

iOS Note: your app ClientID (iosClientId) is always required

// Somewhere in your code
signIn = async () => {
  try {
    const userInfo = await googleAuth.signIn();
    this.setState({ userInfo });
  } catch (error) {
    if (error.code === statusCodes.SIGN_IN_CANCELLED) {
      // user cancelled the login flow
    } else if (error.code === statusCodes.IN_PROGRESS) {
      // operation (f.e. sign in) is in progress already
    } else {
      // some other error happened
    }
  }
};

- signOut()

Remove user session from the device.

signOut = async () => {
  try {
    await googleAuth.revokeAccess();
    await googleAuth.signOut();
    this.setState({ user: null }); // Remember to remove the user from your app's state as well
  } catch (error) {
    console.error(error)
  }
};

- revokeAccess()

Remove your application from the user authorized applications.

googleAuth.revokeAccess()
  .then(() => {
    console.log('deleted');
  })
  .catch(error => {
    console.error(error);
  });

userInfo

Example userInfo which is returned after successful sign in.

{
  idToken: string,
  accessToken: string | null,
  accessTokenExpirationDate: number | null, // DEPRECATED, on iOS it's a time interval since now in seconds, on Android it's always null
  serverAuthCode: string,
  scopes: Array<string>, // on iOS this is empty array if no additional scopes are defined
  user: {
    email: string,
    id: string,
    givenName: string,
    familyName: string,
    photo: string, // url
    name: string // full name
  }
}