|
| 1 | +<?code-excerpt path-base="excerpts/packages/google_identity_services_web_example"?> |
| 2 | + |
| 3 | +# google_identity_services_web |
| 4 | + |
| 5 | +A JS-interop layer for Google Identity's Sign In With Google SDK. |
| 6 | + |
| 7 | +See the original JS SDK reference: |
| 8 | + |
| 9 | +* [Sign In With Google](https://developers.google.com/identity/gsi/web) |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +This package is the Dart JS-interop layer of the new **Sign In With Google** |
| 14 | +SDK. Here's the API references for both of the sub-libraries: |
| 15 | + |
| 16 | +* `id.dart`: [Sign In With Google JavaScript API reference](https://developers.google.com/identity/gsi/web/reference/js-reference) |
| 17 | +* `oauth2.dart`: [Google 3P Authorization JavaScript Library for websites - API reference](https://developers.google.com/identity/oauth2/web/reference/js-reference) |
| 18 | +* `loader.dart`: An (optional) loader mechanism that installs the library and |
| 19 | +resolves a `Future<void>` when it's ready. |
| 20 | + |
| 21 | +### Loading the SDK |
| 22 | + |
| 23 | +There are two ways to load the JS SDK in your app. |
| 24 | + |
| 25 | +#### Modify your index.html (most performant) |
| 26 | + |
| 27 | +The most performant way is to modify your `web/index.html` file to insert a |
| 28 | +script tag [as recommended](https://developers.google.com/identity/gsi/web/guides/client-library). |
| 29 | +Place the `script` tag in the `<head>` of your site, next to the script tag that |
| 30 | +loads `flutter.js`, so the browser can downloaded both in parallel: |
| 31 | + |
| 32 | +<?code-excerpt "../../web/index-with-script-tag.html (script-tag)"?> |
| 33 | +```html |
| 34 | +<head> |
| 35 | +<!-- ··· --> |
| 36 | + <!-- Include the GSI SDK below --> |
| 37 | + <script src="https://accounts.google.com/gsi/client" async defer></script> |
| 38 | + <!-- This script adds the flutter initialization JS code --> |
| 39 | + <script src="flutter.js" defer></script> |
| 40 | +</head> |
| 41 | +``` |
| 42 | + |
| 43 | +#### With the `loadWebSdk` function (on-demand) |
| 44 | + |
| 45 | +An alternative way, that downloads the SDK on demand, is to use the |
| 46 | +**`loadWebSdk`** function provided by the library. A simple location to embed |
| 47 | +this in a Flutter Web only app can be the `main.dart`: |
| 48 | + |
| 49 | +<?code-excerpt "main.dart (use-loader)"?> |
| 50 | +```dart |
| 51 | +import 'package:google_identity_services_web/loader.dart' as gis; |
| 52 | +// ··· |
| 53 | +void main() async { |
| 54 | + await gis.loadWebSdk(); // Load the GIS SDK |
| 55 | + // The rest of your code... |
| 56 | +// ··· |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +(Note that the above won't compile for mobile apps, so if you're developing a |
| 61 | +cross-platform app, you'll probably need to hide the call to `loadWebSdk` |
| 62 | +behind a [conditional import/export](https://dart.dev/guides/libraries/create-library-packages#conditionally-importing-and-exporting-library-files).) |
| 63 | + |
| 64 | +### Using the SDK |
| 65 | + |
| 66 | +Once the SDK has been loaded, it can be used by importing the correct library: |
| 67 | + |
| 68 | +* `import 'package:google_identity_services/id.dart' as id;` for Authentication |
| 69 | +* `import 'package:google_identity_services/oauth2.dart' as oauth2;` for |
| 70 | + Authorization. |
| 71 | + |
| 72 | +## Browser compatibility |
| 73 | + |
| 74 | +The new SDK is introducing concepts that are on track for standardization to |
| 75 | +most browsers, and it might not be compatible with older browsers. |
| 76 | + |
| 77 | +Refer to the official documentation site for the latest browser compatibility |
| 78 | +information of the underlying JS SDK: |
| 79 | + |
| 80 | +* **Sign In With Google > [Supported browsers and platforms](https://developers.google.com/identity/gsi/web/guides/supported-browsers)** |
| 81 | + |
| 82 | +## Testing |
| 83 | + |
| 84 | +This web-only package uses `dart:test` to test its features. They can be run |
| 85 | +with `dart test -p chrome`. |
| 86 | + |
| 87 | +_(Look at `test/README.md` and `tool/run_tests.dart` for more info.)_ |
0 commit comments