Skip to content

Commit 0e3a310

Browse files
committed
docs(auth): document free Credential Manager Google Sign-In via react-native
1 parent 4a300c5 commit 0e3a310

2 files changed

Lines changed: 136 additions & 16 deletions

File tree

docs/auth/social-auth.mdx

Lines changed: 135 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -206,20 +206,112 @@ with the new authentication state of the user.
206206

207207
## Google
208208

209-
The [`google-signin`](https://github.com/react-native-google-signin/google-signin) library provides a wrapper around the official Google login library,
210-
allowing you to create a credential and sign-in to Firebase.
209+
Firebase does not ship a Google sign-in UI. You install a community library to obtain a Google **ID token**, then pass it to `GoogleAuthProvider.credential()` and `signInWithCredential()`.
211210

212211
Ensure the "Google" sign-in provider is enabled on the [Firebase Console](https://console.firebase.google.com/project/_/authentication/providers).
213212

214-
### Configure an Expo project
213+
On **Android**, Google recommends [Credential Manager](https://developer.android.com/identity/sign-in/credential-manager-siwg-implementation) for Sign in with Google. Two maintained React Native libraries can provide that flow:
215214

216-
For Expo projects, follow [the setup instructions for Expo](https://react-native-google-signin.github.io/docs/category/setting-up) from `react-native-google-signin`.
215+
| Library | Credential Manager (Android) | Cost | Notes |
216+
| ---------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
217+
| [`react-native-nitro-google-signin`](https://www.npmjs.com/package/react-native-nitro-google-signin) | Yes | Free (MIT) | Universal / One Tap-style API via [Nitro Modules](https://nitro.margelo.com); requires `react-native-nitro-modules` |
218+
| [`@react-native-google-signin/google-signin`](https://github.com/react-native-google-signin/google-signin) | Yes with [Universal Sign-In](https://react-native-google-signin.github.io/docs/install); **no** on the free public `GoogleSignin` API | Paid for Credential Manager; free for legacy Android SDK | Universal Sign-In uses `GoogleOneTapSignIn`; the free public build is documented in the section below |
217219

218-
### Configure a React-Native (non-Expo) project
220+
The sections below show Firebase integration for each approach. For native setup (OAuth clients, SHA-1, Expo config plugins), follow that library's documentation.
221+
222+
### react-native-nitro-google-signin
223+
224+
[`react-native-nitro-google-signin`](https://www.npmjs.com/package/react-native-nitro-google-signin) is a free, MIT-licensed option that uses **Credential Manager** on Android and the Google Sign-In SDK on iOS. It is a good fit when you want modern Android sign-in without a paid Universal Sign-In license from `@react-native-google-signin/google-signin`.
225+
226+
Install the package and its peer dependency:
227+
228+
```bash
229+
yarn add react-native-nitro-google-signin react-native-nitro-modules
230+
```
231+
232+
Then follow the [installation](https://react-native-nitro-google-signin.github.io/docs/getting-started/installation) and [Google Cloud setup](https://react-native-nitro-google-signin.github.io/docs/setup/google-cloud) guides. For Expo, use a [development build](https://docs.expo.dev/develop/development-builds/introduction/) and the [Expo config plugin](https://react-native-nitro-google-signin.github.io/docs/setup/expo) — it does not run in Expo Go.
233+
234+
#### Using Google Sign-In with Firebase
235+
236+
Configure once (with Firebase config files present, `webClientId: 'autoDetect'` reads the Web client ID from `google-services.json` / `GoogleService-Info.plist`):
237+
238+
```js
239+
import { GoogleOneTapSignIn } from 'react-native-nitro-google-signin';
240+
241+
GoogleOneTapSignIn.configure({ webClientId: 'autoDetect' });
242+
```
243+
244+
Trigger sign-in from your UI, then exchange the ID token with Firebase:
245+
246+
```jsx
247+
import { Button } from 'react-native';
248+
249+
function GoogleSignIn() {
250+
return (
251+
<Button
252+
title="Google Sign-In"
253+
onPress={() => onGoogleButtonPress().then(() => console.log('Signed in with Google!'))}
254+
/>
255+
);
256+
}
257+
```
258+
259+
```js
260+
import { GoogleAuthProvider, getAuth, signInWithCredential } from '@react-native-firebase/auth';
261+
import {
262+
GoogleOneTapSignIn,
263+
isNoSavedCredentialFoundResponse,
264+
isSuccessResponse,
265+
} from 'react-native-nitro-google-signin';
266+
267+
async function onGoogleButtonPress() {
268+
await GoogleOneTapSignIn.checkPlayServices();
269+
270+
let response = await GoogleOneTapSignIn.signIn();
271+
272+
if (isNoSavedCredentialFoundResponse(response)) {
273+
response = await GoogleOneTapSignIn.createAccount();
274+
}
275+
if (isNoSavedCredentialFoundResponse(response)) {
276+
response = await GoogleOneTapSignIn.presentExplicitSignIn();
277+
}
278+
279+
if (!isSuccessResponse(response)) {
280+
throw new Error('Google Sign-In was cancelled or failed');
281+
}
282+
283+
const { idToken } = response.data;
284+
if (!idToken) {
285+
throw new Error('No ID token found');
286+
}
287+
288+
const googleCredential = GoogleAuthProvider.credential(idToken);
289+
return signInWithCredential(getAuth(), googleCredential);
290+
}
291+
```
292+
293+
Upon successful sign-in, any [`onAuthStateChanged`](/auth/usage#listening-to-authentication-state) listeners will trigger with the new authentication state.
294+
295+
If you test on an Android emulator, use an image with **Google APIs** or **Google Play** system images.
296+
297+
Full API details: [react-native-nitro-google-signin documentation](https://react-native-nitro-google-signin.github.io/).
298+
299+
### @react-native-google-signin/google-signin
300+
301+
The [`@react-native-google-signin/google-signin`](https://github.com/react-native-google-signin/google-signin) package is widely used and offers two tiers:
302+
303+
- **Universal Sign-In** ([paid](https://universal-sign-in.com/#pricing)): Cross-platform One Tap-style API (`GoogleOneTapSignIn`) with **Credential Manager** on Android. Licensed builds are installed from the package maintainer's registry — see [their installation guide](https://react-native-google-signin.github.io/docs/install).
304+
- **Public (free) version**: `GoogleSignin` API for Android and iOS using the **legacy** Android Google Sign-In SDK. Google has deprecated that stack; it does not use Credential Manager. The examples below use this free API.
305+
306+
#### Configure an Expo project
307+
308+
For Expo projects, follow [the setup instructions for Expo](https://react-native-google-signin.github.io/docs/category/setting-up) from `@react-native-google-signin/google-signin`.
309+
310+
#### Configure a React-Native (non-Expo) project
219311

220312
For bare React-Native projects, most configuration is already setup when using Google Sign-In with React-Native-Firebase's configuration, however you need to ensure your machines SHA1 key has been configured for use with Android. You can see how to generate the key on the [Getting Started](/) documentation.
221313

222-
### Using Google Sign-In
314+
#### Using Google Sign-In
223315

224316
Before triggering a sign-in request, you must initialize the Google SDK with any required scopes and the
225317
`webClientId`, which can be found in the `android/app/google-services.json` file as the `client/oauth_client/client_id` property (the id ends with `.apps.googleusercontent.com`). Make sure to pick the `client_id` with `client_type: 3`
@@ -282,6 +374,8 @@ with the new authentication state of the user.
282374
283375
If you are testing this feature on an android emulator ensure that the emulate is either the Google APIs or Google Play flavor.
284376
377+
> If you need Credential Manager on Android without a paid license, use [`react-native-nitro-google-signin`](#react-native-nitro-google-signin) instead of upgrading to Universal Sign-In.
378+
285379
## Microsoft
286380
287381
Per the [documentation](https://firebase.google.com/docs/auth/android/microsoft-oauth#expandable-1), we cannot handle the Sign-In flow manually, by getting the access token from a library such as `react-native-app-auth`, and then calling `signInWithCredential`.
@@ -406,22 +500,48 @@ To achieve this, you should replace sign-in method in any of the supported socia
406500
407501
This code demonstrates linking a Google provider to an account that is already signed in using Firebase authentication.
408502
503+
With [`react-native-nitro-google-signin`](https://www.npmjs.com/package/react-native-nitro-google-signin):
504+
505+
```js
506+
import { GoogleAuthProvider, getAuth } from '@react-native-firebase/auth';
507+
import {
508+
GoogleOneTapSignIn,
509+
isNoSavedCredentialFoundResponse,
510+
isSuccessResponse,
511+
} from 'react-native-nitro-google-signin';
512+
513+
async function onGoogleLinkButtonPress() {
514+
await GoogleOneTapSignIn.checkPlayServices();
515+
516+
let response = await GoogleOneTapSignIn.signIn();
517+
if (isNoSavedCredentialFoundResponse(response)) {
518+
response = await GoogleOneTapSignIn.presentExplicitSignIn();
519+
}
520+
if (!isSuccessResponse(response) || !response.data.idToken) {
521+
throw new Error('Google Sign-In failed');
522+
}
523+
524+
const googleCredential = GoogleAuthProvider.credential(response.data.idToken);
525+
await getAuth().currentUser.linkWithCredential(googleCredential);
526+
}
527+
```
528+
529+
With the free `@react-native-google-signin/google-signin` (`GoogleSignin`) API:
530+
409531
```js
410532
import { GoogleAuthProvider, getAuth } from '@react-native-firebase/auth';
411533
import { GoogleSignin } from '@react-native-google-signin/google-signin';
412534

413535
async function onGoogleLinkButtonPress() {
414-
// Ensure the device supports Google Play services
415536
await GoogleSignin.hasPlayServices({ showPlayServicesUpdateDialog: true });
416-
// Obtain the user's ID token
417-
const { idToken } = await GoogleSignin.signIn();
537+
const signInResult = await GoogleSignin.signIn();
418538

419-
// Create a Google credential with the token
420-
const googleCredential = GoogleAuthProvider.credential(idToken);
539+
const idToken = signInResult.data?.idToken ?? signInResult.idToken;
540+
if (!idToken) {
541+
throw new Error('No ID token found');
542+
}
421543

422-
// Link the user's account with the Google credential
423-
const firebaseUserCredential = await getAuth().currentUser.linkWithCredential(googleCredential);
424-
// Handle the linked account as needed in your app
425-
return;
544+
const googleCredential = GoogleAuthProvider.credential(idToken);
545+
await getAuth().currentUser.linkWithCredential(googleCredential);
426546
}
427547
```

docs/auth/usage/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ signOut(getAuth()).then(() => console.log('User signed out!'));
185185
Once successfully signed out, any [`onAuthStateChanged`](/auth/usage#listening-to-authentication-state) listeners will trigger an event
186186
with the `user` parameter being a `null` value.
187187

188-
Additionally, using `GoogleSignin.revokeAccess()` forgets the user. This means that the next time someone signs in, they will see the account selection popup. If you don't use this function, the last account will be automatically used without showing the account selection popup.
188+
Additionally, calling `revokeAccess()` on your Google sign-in library forgets the user on the device (for example `GoogleOneTapSignIn.revokeAccess()` from [`react-native-nitro-google-signin`](https://www.npmjs.com/package/react-native-nitro-google-signin), or `GoogleSignin.revokeAccess()` from `@react-native-google-signin/google-signin`). The next sign-in will show the account picker again. Without it, the last account may be reused automatically.
189189

190190
## Other sign-in methods
191191

0 commit comments

Comments
 (0)