Skip to content

react-native-auth/google

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
react-native-auth logo

πŸ” @react-native-auth/google

Modern Google Authentication for React Native

NPM Version License: MIT Platform: Android

GitHub Issues or Pull Requests GitHub Issues or Pull Requests


πŸ“‹ Table of Contents


✨ Features

🎯 Modern Authentication

  • βœ… One Tap Sign-In (Credential Manager)
  • βœ… Account Chooser UI
  • βœ… Legacy OAuth Support

⚑ Built with Latest Tech

  • βœ… TurboModule (New Architecture)
  • βœ… Codegen Type Safety
  • βœ… Android Only (for now)

πŸ“‹ Available Methods

Method Description Use Case
oneTap() One Tap / Credential Manager Quick sign-in for returning users
signIn() Account Chooser UI First-time sign-in, account selection
legacySignIn() Legacy OAuth with scopes Advanced features (Drive, Calendar, etc.)
signOut() Sign out current user Logout, clear session

πŸ“¦ Installation

# Using npm
npm install @react-native-auth/google

# Using yarn
yarn add @react-native-auth/google

βš™οΈ Setup

πŸ“± Step 1: Enable New Architecture

Show details

Add to your android/gradle.properties:

newArchEnabled=true
kotlin.version=2.1.20

☁️ Step 2: Google Cloud Console Setup

Show details

🌐 Access Console

Go to Google Cloud Console and create/select your project.

πŸ”Œ Enable API

  1. Navigate to APIs & Services β†’ Library
  2. Search for "Google Sign-In API" or "Google Identity"
  3. Click Enable

πŸ”‘ Create OAuth 2.0 Credentials

You need TWO client IDs:

a) πŸ“± Android Client ID

For app verification (SHA-1 fingerprint required)

  1. Go to APIs & Services β†’ Credentials

  2. Click Create Credentials β†’ OAuth 2.0 Client ID

  3. Select Android

  4. Fill in:

    • Name: Your App (Android)

    • Package name: com.yourapp (from android/app/build.gradle)

    • SHA-1 fingerprint: Get it by running:

      # Debug
      keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
      
      # Release
      keytool -list -v -keystore /path/to/release.keystore -alias your-alias
  5. Click Create

b) 🌐 Web Client ID

For authentication (USE THIS IN YOUR CODE)

  1. Click Create Credentials β†’ OAuth 2.0 Client ID again
  2. Select Web application
  3. Fill in:
    • Name: Your App (Web)
    • Authorized redirect URIs: Leave empty
  4. Click Create
  5. Copy the Web Client ID β†’ xxxxx.apps.googleusercontent.com

πŸ’‘ Pro Tip: The Android Client ID verifies your app. The Web Client ID is used for authentication.


πŸš€ Usage

πŸ“₯ Import

import { oneTap, signIn, legacySignIn, signOut } from '@react-native-auth/google';

🎯 One Tap Sign-In

Quick authentication for returning users with saved credentials.

const CLIENT_ID = 'YOUR_WEB_CLIENT_ID.apps.googleusercontent.com';

try {
  const result = await oneTap({ clientId: CLIENT_ID });
  console.log('βœ… Signed in:', result.idToken);
  console.log('πŸ“§ Email:', result.email);
} catch (error) {
  console.error('❌ Sign-in failed:', error);
}

πŸ‘€ Sign-In with Account Chooser

Display account picker UI for first-time users or account switching.

const result = await signIn({
  clientId: CLIENT_ID,
});

πŸ” Legacy OAuth Sign-In

Advanced authentication with custom OAuth scopes (Drive, Calendar, etc.)

const result = await legacySignIn({
  clientId: CLIENT_ID,
  scopes: [
    'https://www.googleapis.com/auth/drive.readonly',
    'https://www.googleapis.com/auth/calendar.readonly',
  ],
  prompt: 'consent', // Optional: Force consent screen
});

πŸ“ prompt Parameter

Controls the authentication flow behavior:

Value Behavior
undefined Default behavior - no forced interaction
'consent' Always show consent screen, even for existing app
'login' Always show login screen, force account selection
'select_account' Always show account selection screen
'none' Silent authentication (may fail if no session)

Example - Force Consent Screen:

const result = await legacySignIn({
  clientId: CLIENT_ID,
  scopes: ['https://www.googleapis.com/auth/calendar'],
  prompt: 'consent', // Useful when updating permissions
});

Example - Force Account Selection:

const result = await legacySignIn({
  clientId: CLIENT_ID,
  prompt: 'select_account', // Always show account picker
});

Example - Force Login:

const result = await legacySignIn({
  clientId: CLIENT_ID,
  prompt: 'login', // Useful for switching accounts
});

πŸšͺ Sign Out

Sign out the current user and clear the session.

try {
  await signOut();
  console.log('βœ… Signed out successfully');
} catch (error) {
  console.error('❌ Sign-out failed:', error);
}

πŸ“ API Reference

Types

type GoogleAuthOptions = {
  clientId: string; // Your Web Client ID
  scopes?: string[]; // OAuth scopes (legacySignIn only)
  prompt?: string; // Consent behavior: 'consent', 'login', 'none'
};

type GoogleAuthResult = {
  idToken: string; // JWT ID token
  email?: string; // User email (if available)
};

πŸ› οΈ Troubleshooting

Common Issues

❌ "Sign-in failed" or "Invalid client ID"

  • βœ… Verify you're using the Web Client ID, not Android Client ID
  • βœ… Check SHA-1 fingerprint is correctly added for Android Client ID
  • βœ… Ensure Google Sign-In API is enabled in Console

❌ "Credential Manager not available"

  • βœ… Test on Android 9+ (API level 28+)
  • βœ… Ensure Google Play Services is updated
  • βœ… Test on a physical device (emulators may have issues)

πŸ“„ License

License: MIT


Made with ❀️ for React Native

Report Bug Β· Request Feature