Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: document new api #23

Merged
merged 10 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ yarn-error.log*
.yarn/*
!.yarn/releases
!.yarn/plugins
!.yarn/patches

.idea
14 changes: 14 additions & 0 deletions .yarn/patches/typedoc-plugin-markdown-npm-4.2.6-525bc81e4a.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
diff --git a/dist/theme/context/partials/member.typeDeclarationTable.js b/dist/theme/context/partials/member.typeDeclarationTable.js
index fb4c85bd702f1d1b165caeecfea32ca54150b614..f0ab06f6d7d3b90cea01e977f190d5bf00cf47bb 100644
--- a/dist/theme/context/partials/member.typeDeclarationTable.js
+++ b/dist/theme/context/partials/member.typeDeclarationTable.js
@@ -29,7 +29,8 @@ function typeDeclarationTable(model, options) {
const rows = [];
declarations.forEach((declaration) => {
const row = [];
- row.push((0, markdown_1.backTicks)(declaration.name));
+ const name = declaration.name+ (declaration.flags.isOptional ? "?" : '')
+ row.push((0, markdown_1.backTicks)(name));
row.push(this.partials.someType(declaration.type));
if (hasDefaultValues) {
row.push((0, utils_1.escapeChars)(!declaration.defaultValue || declaration.defaultValue === '...'
16 changes: 16 additions & 0 deletions docs/_oneTapModuleTable.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
> `const` **GoogleOneTapSignIn**: complex type, see below

The entry point of the One-tap Sign In API, exposed as `GoogleOneTapSignIn`.

On the Web, the signatures of `signIn`, `presentExplicitSignIn`, and `createAccount` are callback-based and on native they are Promise-based. Read more in the [guide](/docs/one-tap#web-support).

#### Type declaration

| Name | Type |
| ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `configure` | (`params`: [`OneTapConfigureParams`](index.mdx#onetapconfigureparams)) => `void` |
| `signIn` | (`params?`: [`OneTapSignInParams`](index.mdx#onetapsigninparams)) => `Promise`\<[`OneTapResponse`](index.mdx#onetapresponse)\> |
| `createAccount` | (`params?`: [`OneTapCreateAccountParams`](index.mdx#onetapcreateaccountparams)) => `Promise`\<[`OneTapResponse`](index.mdx#onetapresponse)\> |
| `presentExplicitSignIn` | (`params?`: [`OneTapExplicitSignInParams`](index.mdx#onetapexplicitsigninparams)) => `Promise`\<[`OneTapExplicitSignInResponse`](index.mdx#onetapexplicitsigninresponse)\> |
| `requestAuthorization` | (`options`: [`RequestAuthorizationParams`](index.mdx#requestauthorizationparams)) => `Promise`\<[`AuthorizationResponse`](index.mdx#authorizationresponse)\> |
| `signOut` | (`emailOrUniqueId`: `string`) => `Promise`\<`null`\> |
14 changes: 14 additions & 0 deletions docs/_utilityFunctions.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
### Utility Functions

:::tip

There are 4 helper functions available:

:::

- [`isErrorWithCode`](errors#iserrorwithcodevalue) for processing errors
- [`isSuccessResponse`](api#issuccessresponse) for checking if a response represents a successful operation. Same as checking `response.type === 'success'`.
- [`isNoSavedCredentialFoundResponse`](api#isnosavedcredentialfoundresponse) for checking if a response represents no saved credentials case. Same as checking `response.type === 'noSavedCredentialFound'`.
- [`isCancelledResponse`](api#iscancelledresponse) for checking if a response represents user cancellation case. Same as checking `response.type === 'cancelled'`.

---
2 changes: 0 additions & 2 deletions docs/api/_category_.yml

This file was deleted.

352 changes: 0 additions & 352 deletions docs/api/index.md

This file was deleted.

578 changes: 578 additions & 0 deletions docs/api/index.mdx

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions docs/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ sidebar_position: 60

# Error handling

When `catch`ing errors thrown by the library, it's strongly recommended not to immediately present them using the [`Alert` module](https://reactnative.dev/docs/alert). This is because on Android, when transitioning from the Google Sign-In flow to your app, the current [Activity](https://developer.android.com/reference/android/app/Activity) may be `null` which would cause the alert call to be a noop. You can work around this by presenting the alert after a delay, or handling the error differently.

### `isErrorWithCode(value)`

TypeScript helper to check if the passed parameter is an instance of `Error` which has the `code` property. All errors thrown by this library have the `code` property, which contains a value from [`statusCodes`](#status-codes) or some other string for the less-usual errors.
Expand Down Expand Up @@ -40,16 +42,13 @@ See [example usage](original#signin).

| Name | Description |
| ----------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `SIGN_IN_CANCELLED` | When user cancels the sign in flow. On the Web, this is also returned while [cooldown period](https://developers.google.com/identity/gsi/web/guides/features#exponential_cooldown) is active. Detecting the cooldown period itself is not possible on the Web for user privacy reasons. On Android, it can be detected via `ONE_TAP_START_FAILED`. |
| `IN_PROGRESS` | Trying to invoke another operation (e.g. `signInSilently`) when previous one has not yet finished. If you call e.g. `signInSilently` twice, two calls to `signInSilently` in the native module will be done. The promise from the first call to `signInSilently` will be rejected with this error, and the second will resolve / reject with the result of the native call. |
| `SIGN_IN_REQUIRED` | Useful for use with `signInSilently()` - no user has signed in yet. This error does not happen with one-tap sign in (instead see `NO_SAVED_CREDENTIAL_FOUND`). |
| `PLAY_SERVICES_NOT_AVAILABLE` | Play services are not available or outdated. This happens on Android, or on the Web when you're calling the exposed APIs [before the Client library is loaded](setting-up/web). |

### Status codes specific to One-tap sign in

| Name | Description |
| --------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `ONE_TAP_START_FAILED` | Thrown only on Android or Web when the One-tap sign in UI cannot be presented. On Android, this happens during the [cooldown period](https://developers.google.com/identity/gsi/web/guides/features#exponential_cooldown). You can still call `presentExplicitSignIn` on Android or render `WebGoogleSigninButton` on Web as a fallback. |
| `NO_SAVED_CREDENTIAL_FOUND` | Android and Apple only (Web shows the one-tap UI instead). Thrown when no user signed in to your app yet. To recover from this error, proceed to calling [`createAccount`](one-tap#createaccount). |
| Name | Description |
| ---------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `ONE_TAP_START_FAILED` | Thrown only on Android when the One-tap sign in UI cannot be presented. This happens during the [cooldown period](https://developers.google.com/identity/gsi/web/guides/features#exponential_cooldown). You can still call `presentExplicitSignIn` in that case. |

See [example usage](one-tap#signin).
14 changes: 9 additions & 5 deletions docs/install.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@ Available exclusively to GitHub sponsors, this version offers:

✅ One-tap sign-in support for [Android](https://developers.google.com/identity/android-credential-manager) and [Web](https://developers.google.com/identity/gsi/web/guides/features), with an API compatibility layer for iOS and macOS.

✅ [Automatic webClientId detection](one-tap#automatic-webclientid-detection) for Firebase.

✅ Support for macOS (using react-native-macOS).

Your sponsorship enables continued maintenance and development of the module and contributions to upstream SDKs [1](https://github.com/openid/AppAuth-iOS/pull/788), [2](https://github.com/google/GoogleSignIn-iOS/pull/402). Thank you for supporting this project! ❤️
Your sponsorship enables continued maintenance and development of the module and contributions to upstream SDKs (such as [1](https://github.com/openid/AppAuth-iOS/pull/788), [2](https://github.com/google/GoogleSignIn-iOS/pull/402)). Thank you for supporting this project! ❤️

### Public Version

Available on the public npm registry, this version does not include One-tap sign-in (it uses the deprecated Google Sign-In for Android), Web and macOS support found in the sponsor-only version.

## Accessing the private package for sponsors

[Upon sponsoring](https://github.com/sponsors/vonovak) (please use specifically the tiers that mention this project!), you will get access to the private package and to the private repo with the sources and examples.
[Upon sponsoring](https://github.com/sponsors/vonovak) (please use specifically the tiers that mention this project!), you will get an invitation to access the private package and to the private repo with the sources and examples.

Three steps are needed to access the sponsor package, which is hosted on [GitHub npm packages registry](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-npm-registry).

1. [Become a sponsor](https://github.com/sponsors/vonovak) and accept private repository invitation - it takes just a few clicks.
1. [Become a sponsor](https://github.com/sponsors/vonovak) and accept the private repository invitation - it takes just a few clicks.

2. [Obtain here](https://github.com/settings/tokens) a Personal Access Token with `packages:read` permission.

Expand Down Expand Up @@ -67,14 +69,16 @@ If you use another package manager ([such as bun](https://bun.sh/docs/install/re

The package requires React Native 0.60 or higher.

If you're using the [New Architecture](https://reactnative.dev/docs/new-architecture-intro), you need to be on React Native 0.72 or higher.
If you're using the [New Architecture](https://reactnative.dev/docs/new-architecture-intro), you need to be on React Native 0.72 or higher but it's strongly recommended to use the latest version available.

## Installing

```bash
yarn add @react-native-google-signin/google-signin
yarn add @react-native-google-signin/google-signin@latest
```

If you're a sponsor, open the npm lockfile (`yarn.lock` or `package-lock.json`) and verify that the package is fetched from the GitHub registry (the entry will point to `npm.pkg.github.com`, not `registry.npmjs.org`). If it's not, it means that your package manager is not configured correctly.

There are several guides to follow now:

- [Expo guide](setting-up/expo) for native mobile apps built with Expo
Expand Down
90 changes: 90 additions & 0 deletions docs/migrating.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
sidebar_position: 90
sidebar_label: Migrating
---

# Migrating to new JS API

## One-tap sign in module

1. Add the [`configure`](one-tap#configure) method to your code. This method is required to be called to configure the module.

2. Change the `signIn`, `createAccount`, `presentExplicitSignIn`, and `requestAuthorization` methods to use the new apis built on [`OneTapResponse`](/docs/api#onetapresponse):

```diff
const signIn = async () => {
try {
- const userInfo = await GoogleOneTapSignIn.signIn({
- webClientId: `autoDetect`, // works only if you use Firebase
- iosClientId: config.iosClientId, // only needed if you're not using Firebase
- });
- setState({ userInfo });
+ const response = await GoogleOneTapSignIn.signIn();
+
+ if (response.type === 'success') {
+ setState({ userInfo: response.data });
+ } else if (response.type === 'noSavedCredentialFound') {
+ // Android and Apple only. No saved credential found, call `createAccount`
+ }

} catch (error) {
if (isErrorWithCode(error)) {
switch (error.code) {
- case statusCodes.NO_SAVED_CREDENTIAL_FOUND:
- // Android and Apple only. No saved credential found, call `createAccount`
- break;
- case statusCodes.SIGN_IN_CANCELLED:
- // sign in was cancelled
- break;
case statusCodes.ONE_TAP_START_FAILED:
// Android-only, you probably have hit rate limiting.
// On Android, you can still call `presentExplicitSignIn` in this case.
break;
case statusCodes.PLAY_SERVICES_NOT_AVAILABLE:
// Android-only: play services not available or outdated
// Web: when calling an unimplemented api (requestAuthorization)
break;
default:
// something else happened
}
} else {
// an error that's not related to google sign in occurred
}
}
};
```

3. If requesting offline access in `requestAuthorization` on Android, add `enabled: true`:

```diff
await GoogleOneTapSignIn.requestAuthorization({
offlineAccess: {
+ enabled: true,
},
});
```

### Original Sign In

1. Follow step 2. from above for `signIn`, `addScopes` and `signInSilently` methods.
2. remove `SIGN_IN_REQUIRED` mentions. This case is now handled with [`NoSavedCredentialFound`](api#nosavedcredentialfound) object:

```diff
const getCurrentUserInfo = async () => {
try {
const response = await GoogleSignin.signInSilently();
+ if (isSuccessResponse(response)) {
+ setState({ userInfo: response.data })
+ } else if (isNoSavedCredentialFoundResponse(response)) {
+ // user has not signed in yet
+ }
- setState({ userInfo: response });
} catch (error) {
- if (error.code === statusCodes.SIGN_IN_REQUIRED) {
- // user has not signed in yet
- } else {
- // some other error
- }
}
};
```
Loading