clay-google-signin
v0.1.12
Published
My new module
Maintainers
Readme
Clay Google Sign-in
A React Native module for Google Sign-in. This module works for both iOS and Android.
Installation
npm install clay-google-signinor
yarn add clay-google-signinThis library comes with a config plugin that handles the native project configuration for you.
Configuration
iOS
Follow the Google Sign-In for iOS integration guide to get your
GoogleService-Info.plistfile.Place this file in the root of your project (e.g., alongside
App.js). The config plugin will automatically copy this file to the native project and configure the required URL Schemes and Client ID in yourInfo.plist.If you need to authenticate with a backend server, you can pass your server's client ID to the plugin. Add the following to your
app.jsonorapp.config.js:// app.json { "expo": { "plugins": [ [ "clay-google-signin", { "serverClientId": "YOUR_SERVER_CLIENT_ID_HERE" } ] ] } }Run
npx expo prebuildto sync the native project files.
Android
- Go to your project in the Firebase console.
- In your project settings, download the
google-services.jsonfile. - Place this file in the root of your project (e.g., alongside
App.js). - The config plugin will automatically copy the file and configure Gradle during the prebuild process.
- Run
expo prebuildto sync the changes.
Usage
First, you need to configure the module, usually in your app's entry file.
import { GoogleSignin } from 'clay-google-signin';
GoogleSignin.configure({
webClientId: 'YOUR_WEB_CLIENT_ID', // client ID of type WEB for your server (needed to verify user ID and for offline access)
iosClientId: 'YOUR_IOS_CLIENT_ID', // found in your GoogleService-Info.plist
});Then, you can use the GoogleSigninButton component and the GoogleSignin API.
import { GoogleSignin, GoogleSigninButton, User } from 'clay-google-signin';
import React, { useState } from 'react';
import { View, Text, Button, Alert } from 'react-native';
function App() {
const [user, setUser] = useState<User | null>(null);
const signIn = async () => {
try {
await GoogleSignin.hasPlayServices();
const userInfo = await GoogleSignin.signIn();
setUser(userInfo);
} catch (error: any) {
if (error.code !== '-5') { // -5 is user cancellation
Alert.alert("Sign In Error", error.message);
}
}
};
const signOut = async () => {
try {
await GoogleSignin.signOut();
setUser(null);
} catch (error: any) {
Alert.alert('Sign Out Error', error.message);
}
};
if (!user) {
return (
<View>
<GoogleSigninButton onPress={signIn} />
</View>
);
}
return (
<View>
<Text>Welcome, {user.name}</Text>
<Button title="Sign Out" onPress={signOut} />
</View>
)
}API
The GoogleSignin object provides the following methods:
hasPlayServices(params?: { showPlayServicesUpdateDialog: boolean }): Promise<boolean>(Android only)configure(params: ConfigureParams): Promise<void>signIn(): Promise<User>signInSilently(): Promise<User | null>signOut(): Promise<void>revokeAccess(): Promise<void>hasPreviousSignIn(): Promise<boolean>getCurrentUser(): Promise<User | null>getTokens(): Promise<{ idToken: string; accessToken: string }>addScopes(scopes: string[]): Promise<User>clearCachedAccessToken(token: string): Promise<void>
See src/ClayGoogleSignin.types.ts for the shape of the User object and ConfigureParams.
Troubleshooting
Android Error 12502
This error indicates a mismatch between your app's signing certificate and the SHA-1 fingerprint registered in your Firebase project.
Step 1: Get the SHA-1 Fingerprint
- Navigate to the
androiddirectory of your project (e.g.,cd android). - Run the
./gradlew signingReportcommand. - In the output, find the
SHA-1value for thedebugvariant and copy it.
Step 2: Add the Fingerprint to Firebase
- Open your project in the Firebase console.
- Go to Project settings > General tab.
- Scroll down to "Your apps" and select your Android app.
- Click "Add fingerprint" and paste the SHA-1 key you copied.
- Save your changes. It may take a few minutes for the new settings to apply.
- Rebuild your app.
