From 666a98189bd6d141538ed0baaeea46305fa64a69 Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 29 Nov 2023 14:33:01 -0800 Subject: [PATCH 1/4] [google_sign_in] Adopt code excerpts in README --- .../google_sign_in/CHANGELOG.md | 3 ++- .../google_sign_in/google_sign_in/README.md | 24 ++++++++++++------- .../google_sign_in/example/lib/main.dart | 12 ++++++++++ .../google_sign_in/pubspec.yaml | 2 +- script/configs/temp_exclude_excerpt.yaml | 1 - 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/packages/google_sign_in/google_sign_in/CHANGELOG.md b/packages/google_sign_in/google_sign_in/CHANGELOG.md index e18b832ab9e..9dbe82fd80d 100644 --- a/packages/google_sign_in/google_sign_in/CHANGELOG.md +++ b/packages/google_sign_in/google_sign_in/CHANGELOG.md @@ -1,6 +1,7 @@ -## NEXT +## 6.1.7 * Updates minimum supported SDK version to Flutter 3.10/Dart 3.0. +* Improves README example and updates it to use code excerpts. ## 6.1.6 diff --git a/packages/google_sign_in/google_sign_in/README.md b/packages/google_sign_in/google_sign_in/README.md index 87f28571805..5ee58295090 100644 --- a/packages/google_sign_in/google_sign_in/README.md +++ b/packages/google_sign_in/google_sign_in/README.md @@ -66,18 +66,24 @@ To use this plugin, follow the Add the following import to your Dart code: + ```dart import 'package:google_sign_in/google_sign_in.dart'; ``` Initialize `GoogleSignIn` with the scopes you want: + ```dart +const List scopes = [ + 'email', + 'https://www.googleapis.com/auth/contacts.readonly', +]; + GoogleSignIn _googleSignIn = GoogleSignIn( - scopes: [ - 'email', - 'https://www.googleapis.com/auth/contacts.readonly', - ], + // Optional clientId + // clientId: 'your-client_id.apps.googleusercontent.com', + scopes: scopes, ); ``` @@ -85,6 +91,7 @@ GoogleSignIn _googleSignIn = GoogleSignIn( You can now use the `GoogleSignIn` class to authenticate in your Dart code, e.g. + ```dart Future _handleSignIn() async { try { @@ -118,8 +125,9 @@ Applications must be able to: There's a new method that enables the checks above, `canAccessScopes`: + ```dart -final bool isAuthorized = await _googleSignIn.canAccessScopes(scopes); +isAuthorized = await _googleSignIn.canAccessScopes(scopes); ``` _(Only implemented in the web platform, from version 6.1.0 of this package)_ @@ -130,14 +138,14 @@ If an app determines that the user hasn't granted the scopes it requires, it should initiate an Authorization request. (Remember that in the web platform, this request **must be initiated from an user interaction**, like a button press). + ```dart Future _handleAuthorizeScopes() async { final bool isAuthorized = await _googleSignIn.requestScopes(scopes); + // Do things that only authorized users can do! if (isAuthorized) { - // Do things that only authorized users can do! - _handleGetContact(_currentUser!); + unawaited(_handleGetContact(_currentUser!)); } -} ``` The `requestScopes` returns a `boolean` value that is `true` if the user has diff --git a/packages/google_sign_in/google_sign_in/example/lib/main.dart b/packages/google_sign_in/google_sign_in/example/lib/main.dart index a103353c797..89bf496286b 100644 --- a/packages/google_sign_in/google_sign_in/example/lib/main.dart +++ b/packages/google_sign_in/google_sign_in/example/lib/main.dart @@ -9,12 +9,15 @@ import 'dart:convert' show json; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; +// #docregion Import import 'package:google_sign_in/google_sign_in.dart'; +// #enddocregion Import import 'package:http/http.dart' as http; import 'src/sign_in_button.dart'; /// The scopes required by this application. +// #docregion Initialize const List scopes = [ 'email', 'https://www.googleapis.com/auth/contacts.readonly', @@ -25,6 +28,7 @@ GoogleSignIn _googleSignIn = GoogleSignIn( // clientId: 'your-client_id.apps.googleusercontent.com', scopes: scopes, ); +// #enddocregion Initialize void main() { runApp( @@ -59,7 +63,9 @@ class _SignInDemoState extends State { bool isAuthorized = account != null; // However, in the web... if (kIsWeb && account != null) { +// #docregion CanAccessScopes isAuthorized = await _googleSignIn.canAccessScopes(scopes); +// #enddocregion CanAccessScopes } setState(() { @@ -136,6 +142,7 @@ class _SignInDemoState extends State { // // On the web, the on-click handler of the Sign In button is owned by the JS // SDK, so this method can be considered mobile only. + // #docregion SignIn Future _handleSignIn() async { try { await _googleSignIn.signIn(); @@ -143,6 +150,7 @@ class _SignInDemoState extends State { print(error); } } + // #enddocregion SignIn // Prompts the user to authorize `scopes`. // @@ -150,14 +158,18 @@ class _SignInDemoState extends State { // and Authorization at the same time (like the web). // // On the web, this must be called from an user interaction (button click). + // #docregion RequestScopes Future _handleAuthorizeScopes() async { final bool isAuthorized = await _googleSignIn.requestScopes(scopes); + // #enddocregion RequestScopes setState(() { _isAuthorized = isAuthorized; }); + // #docregion RequestScopes if (isAuthorized) { unawaited(_handleGetContact(_currentUser!)); } + // #enddocregion RequestScopes } Future _handleSignOut() => _googleSignIn.disconnect(); diff --git a/packages/google_sign_in/google_sign_in/pubspec.yaml b/packages/google_sign_in/google_sign_in/pubspec.yaml index a86b08c2ff3..c10b1d7c1d5 100644 --- a/packages/google_sign_in/google_sign_in/pubspec.yaml +++ b/packages/google_sign_in/google_sign_in/pubspec.yaml @@ -3,7 +3,7 @@ description: Flutter plugin for Google Sign-In, a secure authentication system for signing in with a Google account on Android and iOS. repository: https://github.com/flutter/packages/tree/main/packages/google_sign_in/google_sign_in issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+google_sign_in%22 -version: 6.1.6 +version: 6.1.7 environment: sdk: ">=3.0.0 <4.0.0" diff --git a/script/configs/temp_exclude_excerpt.yaml b/script/configs/temp_exclude_excerpt.yaml index c1fe3e9aa86..95868f3c305 100644 --- a/script/configs/temp_exclude_excerpt.yaml +++ b/script/configs/temp_exclude_excerpt.yaml @@ -10,7 +10,6 @@ - extension_google_sign_in_as_googleapis_auth - flutter_image - go_router_builder -- google_sign_in/google_sign_in - image_picker_for_web - in_app_purchase/in_app_purchase - ios_platform_images From 43295f34ad33763b77c4495562ec9c32c0dee744 Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 29 Nov 2023 15:15:46 -0800 Subject: [PATCH 2/4] update code excerpts --- packages/google_sign_in/google_sign_in/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/google_sign_in/google_sign_in/README.md b/packages/google_sign_in/google_sign_in/README.md index 5ee58295090..2b49a33885d 100644 --- a/packages/google_sign_in/google_sign_in/README.md +++ b/packages/google_sign_in/google_sign_in/README.md @@ -142,7 +142,6 @@ this request **must be initiated from an user interaction**, like a button press ```dart Future _handleAuthorizeScopes() async { final bool isAuthorized = await _googleSignIn.requestScopes(scopes); - // Do things that only authorized users can do! if (isAuthorized) { unawaited(_handleGetContact(_currentUser!)); } From 8f4d87f350f4e57df00e0cd3e535af1328b54d8b Mon Sep 17 00:00:00 2001 From: Mike Date: Mon, 4 Dec 2023 13:52:54 -0800 Subject: [PATCH 3/4] Update code snippets Remove Import snippet since it's in the installation page instructions Expand CanAccessScopes snippet to include additional pertinent information --- packages/google_sign_in/google_sign_in/README.md | 14 ++++++-------- .../google_sign_in/example/lib/main.dart | 6 ++---- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/packages/google_sign_in/google_sign_in/README.md b/packages/google_sign_in/google_sign_in/README.md index 2b49a33885d..7735915e026 100644 --- a/packages/google_sign_in/google_sign_in/README.md +++ b/packages/google_sign_in/google_sign_in/README.md @@ -64,13 +64,6 @@ To use this plugin, follow the ### Use the plugin -Add the following import to your Dart code: - - -```dart -import 'package:google_sign_in/google_sign_in.dart'; -``` - Initialize `GoogleSignIn` with the scopes you want: @@ -127,7 +120,12 @@ There's a new method that enables the checks above, `canAccessScopes`: ```dart -isAuthorized = await _googleSignIn.canAccessScopes(scopes); +// In mobile, being authenticated means being authorized... +bool isAuthorized = account != null; +// However, in the web... +if (kIsWeb && account != null) { + isAuthorized = await _googleSignIn.canAccessScopes(scopes); +} ``` _(Only implemented in the web platform, from version 6.1.0 of this package)_ diff --git a/packages/google_sign_in/google_sign_in/example/lib/main.dart b/packages/google_sign_in/google_sign_in/example/lib/main.dart index 89bf496286b..143e1c4aa0f 100644 --- a/packages/google_sign_in/google_sign_in/example/lib/main.dart +++ b/packages/google_sign_in/google_sign_in/example/lib/main.dart @@ -9,9 +9,7 @@ import 'dart:convert' show json; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; -// #docregion Import import 'package:google_sign_in/google_sign_in.dart'; -// #enddocregion Import import 'package:http/http.dart' as http; import 'src/sign_in_button.dart'; @@ -59,14 +57,14 @@ class _SignInDemoState extends State { _googleSignIn.onCurrentUserChanged .listen((GoogleSignInAccount? account) async { +// #docregion CanAccessScopes // In mobile, being authenticated means being authorized... bool isAuthorized = account != null; // However, in the web... if (kIsWeb && account != null) { -// #docregion CanAccessScopes isAuthorized = await _googleSignIn.canAccessScopes(scopes); -// #enddocregion CanAccessScopes } +// #enddocregion CanAccessScopes setState(() { _currentUser = account; From 2abcb1ec9884ae72e472ef0f1f00370aceb5b7a8 Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 6 Dec 2023 14:12:59 -0800 Subject: [PATCH 4/4] Fix comment wording --- packages/google_sign_in/google_sign_in/README.md | 2 +- packages/google_sign_in/google_sign_in/example/lib/main.dart | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/google_sign_in/google_sign_in/README.md b/packages/google_sign_in/google_sign_in/README.md index 7735915e026..35328ca3241 100644 --- a/packages/google_sign_in/google_sign_in/README.md +++ b/packages/google_sign_in/google_sign_in/README.md @@ -122,7 +122,7 @@ There's a new method that enables the checks above, `canAccessScopes`: ```dart // In mobile, being authenticated means being authorized... bool isAuthorized = account != null; -// However, in the web... +// However, on web... if (kIsWeb && account != null) { isAuthorized = await _googleSignIn.canAccessScopes(scopes); } diff --git a/packages/google_sign_in/google_sign_in/example/lib/main.dart b/packages/google_sign_in/google_sign_in/example/lib/main.dart index 143e1c4aa0f..576ec36feff 100644 --- a/packages/google_sign_in/google_sign_in/example/lib/main.dart +++ b/packages/google_sign_in/google_sign_in/example/lib/main.dart @@ -60,7 +60,7 @@ class _SignInDemoState extends State { // #docregion CanAccessScopes // In mobile, being authenticated means being authorized... bool isAuthorized = account != null; - // However, in the web... + // However, on web... if (kIsWeb && account != null) { isAuthorized = await _googleSignIn.canAccessScopes(scopes); }