Skip to content

Commit b94eb08

Browse files
committed
Some more PR tweaks.
1 parent d15236d commit b94eb08

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

packages/google_sign_in/google_sign_in_web/README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The web implementation of [google_sign_in](https://pub.dev/packages/google_sign_in)
44

5-
## Migrating to v0.11 (Google Identity Services)
5+
## Migrating to v0.11 and v0.12 (Google Identity Services)
66

77
The `google_sign_in_web` plugin is backed by the new Google Identity Services
88
(GIS) JS SDK since version 0.11.0.
@@ -27,12 +27,12 @@ quickly and easily sign users into your app suing their Google accounts.
2727
flows will not return authentication information.
2828
* The GIS SDK no longer has direct access to previously-seen users upon initialization.
2929
* `signInSilently` now displays the One Tap UX for web.
30-
* The GIS SDK only provides an `idToken` (JWT-encoded info) when the user
31-
successfully completes an authentication flow.
30+
* **Since 0.12** The plugin provides an `idToken` (JWT-encoded info) when the
31+
user successfully completes an authentication flow:
3232
* In the plugin: `signInSilently` and through the web-only `renderButton` widget.
3333
* The plugin `signIn` method uses the Oauth "Implicit Flow" to Authorize the requested `scopes`.
3434
* This method only provides an `accessToken`, and not an `idToken`, so if your
35-
app needs an `idToken`, this method **should be avoided**.
35+
app needs an `idToken`, this method **should be avoided on the web**.
3636
* The GIS SDK no longer handles sign-in state and user sessions, it only provides
3737
Authentication credentials for the moment the user did authenticate.
3838
* The GIS SDK no longer is able to renew Authorization sessions on the web.
@@ -73,24 +73,24 @@ for a simple implementation of this (look at the `isAuthorized` variable).)_
7373
Only if the scopes required by an App are different from the
7474
[OpenID Connect scopes](https://developers.google.com/identity/protocols/oauth2/scopes#openid-connect).
7575

76-
If an App only needs an `idToken`, or the OpenID Connect scopes, the "Sign In"
76+
If an App only needs an `idToken`, or the OpenID Connect scopes, the Authentication
7777
bits of the plugin should be enough for your app (`signInSilently` and `renderButton`).
7878

7979
#### What happened to the `signIn` method on the web?
8080

8181
Because the GIS SDK for web no longer provides users with the ability to create
8282
their own Sign-In buttons, or an API to start the sign in flow, the current
83-
implementation of `signIn` (that does authorization and authentication) is impossible
84-
to implement on the web.
83+
implementation of `signIn` (that does authorization and authentication) is no
84+
longer feasible on the web.
8585

8686
The web plugin attempts to simulate the old `signIn` behavior by using the
8787
[Oauth Implicit pop-up flow](https://developers.google.com/identity/oauth2/web/guides/use-token-model), which authenticates and authorizes users.
8888

8989
The drawback of this approach is that the Oauth flow **only returns an `accessToken`**,
9090
and a synthetic version of the User Data, that does **not include an `idToken`**.
9191

92-
The solution to this is to migrate your custom "Sign In" buttons in the web to
93-
the Button Widget provided by this package: `Widget renderButton`.
92+
The solution to this is to **migrate your custom "Sign In" buttons in the web to
93+
the Button Widget provided by this package: `Widget renderButton()`.**
9494

9595
_(Check the [package:google_sign_in example app](https://pub.dev/packages/google_sign_in/example)
9696
for an example on how to mix the `renderButton` widget on the web, with a custom
@@ -152,6 +152,9 @@ so you do not need to add it to your `pubspec.yaml`.
152152
However, if you `import` this package to use any of its APIs directly, you
153153
should add it to your `pubspec.yaml` as usual.
154154

155+
For example, you need to import this package directly if you plan to use the
156+
web-only `Widget renderButton()` method.
157+
155158
### Web integration
156159

157160
First, go through the instructions [here](https://developers.google.com/identity/gsi/web/guides/get-google-api-clientid) to create your Google Sign-In OAuth client ID.

packages/google_sign_in/google_sign_in_web/lib/google_sign_in_web.dart

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class GoogleSignInPlugin extends GoogleSignInPlatform {
5454
@visibleForTesting
5555
StreamController<GoogleSignInUserData?>?
5656
debugOverrideUserDataController,
57-
}) : _gisClient = debugOverrideGisSdkClient,
57+
}) : _gisSdkClient = debugOverrideGisSdkClient,
5858
_userDataController = debugOverrideUserDataController ??
5959
StreamController<GoogleSignInUserData?>.broadcast() {
6060
autoDetectedClientId = html
@@ -81,7 +81,17 @@ class GoogleSignInPlugin extends GoogleSignInPlatform {
8181
final StreamController<GoogleSignInUserData?> _userDataController;
8282

8383
// The instance of [GisSdkClient] backing the plugin.
84-
GisSdkClient? _gisClient;
84+
GisSdkClient? _gisSdkClient;
85+
86+
// A convenience getter to avoid using ! when accessing _gisSdkClient, and
87+
// providing a slightly better error message when it is Null.
88+
GisSdkClient get _gisClient {
89+
assert(_gisSdkClient != null, 'GIS Client not initialized. '
90+
'GoogleSignInPlugin::init() or GoogleSignInPlugin::initWithParams() '
91+
'must be called before any other method in this plugin.',
92+
);
93+
return _gisSdkClient!;
94+
}
8595

8696
// This method throws if init or initWithParams hasn't been called at some
8797
// point in the past. It is used by the [initialized] getter to ensure that
@@ -152,7 +162,7 @@ class GoogleSignInPlugin extends GoogleSignInPlatform {
152162

153163
await _jsSdkLoadedFuture;
154164

155-
_gisClient ??= GisSdkClient(
165+
_gisSdkClient ??= GisSdkClient(
156166
clientId: appClientId!,
157167
hostedDomain: params.hostedDomain,
158168
initialScopes: List<String>.from(params.scopes),
@@ -194,7 +204,7 @@ class GoogleSignInPlugin extends GoogleSignInPlatform {
194204
domDocument.querySelector('#sign_in_button_$viewId');
195205
assert(element != null,
196206
'Cannot render GSI button. DOM is not ready!');
197-
_gisClient!.renderButton(element!, config);
207+
_gisClient.renderButton(element!, config);
198208
});
199209
}
200210
return const Text('Getting ready');
@@ -207,7 +217,7 @@ class GoogleSignInPlugin extends GoogleSignInPlatform {
207217
await initialized;
208218

209219
// The new user is being injected from the `userDataEvents` Stream.
210-
return _gisClient!.signInSilently();
220+
return _gisClient.signInSilently();
211221
}
212222

213223
@override
@@ -221,7 +231,7 @@ class GoogleSignInPlugin extends GoogleSignInPlatform {
221231
// This method will synthesize authentication information from the People API
222232
// if needed (or use the last identity seen from signInSilently).
223233
try {
224-
return _gisClient!.signIn();
234+
return _gisClient.signIn();
225235
} catch (reason) {
226236
throw PlatformException(
227237
code: reason.toString(),
@@ -239,50 +249,50 @@ class GoogleSignInPlugin extends GoogleSignInPlatform {
239249
}) async {
240250
await initialized;
241251

242-
return _gisClient!.getTokens();
252+
return _gisClient.getTokens();
243253
}
244254

245255
@override
246256
Future<void> signOut() async {
247257
await initialized;
248258

249-
_gisClient!.signOut();
259+
_gisClient.signOut();
250260
}
251261

252262
@override
253263
Future<void> disconnect() async {
254264
await initialized;
255265

256-
_gisClient!.disconnect();
266+
_gisClient.disconnect();
257267
}
258268

259269
@override
260270
Future<bool> isSignedIn() async {
261271
await initialized;
262272

263-
return _gisClient!.isSignedIn();
273+
return _gisClient.isSignedIn();
264274
}
265275

266276
@override
267277
Future<void> clearAuthCache({required String token}) async {
268278
await initialized;
269279

270-
_gisClient!.clearAuthCache();
280+
_gisClient.clearAuthCache();
271281
}
272282

273283
@override
274284
Future<bool> requestScopes(List<String> scopes) async {
275285
await initialized;
276286

277-
return _gisClient!.requestScopes(scopes);
287+
return _gisClient.requestScopes(scopes);
278288
}
279289

280290
@override
281291
Future<bool> canAccessScopes(List<String> scopes,
282292
{String? accessToken}) async {
283293
await initialized;
284294

285-
return _gisClient!.canAccessScopes(scopes, accessToken);
295+
return _gisClient.canAccessScopes(scopes, accessToken);
286296
}
287297

288298
@override

0 commit comments

Comments
 (0)