Skip to content

Commit 12b3d59

Browse files
authored
[google_sign_in] Adopt code excerpts in README (flutter#5521)
Improves README example and updates it to use code excerpts. Part of [flutter#102679](flutter#102679)
1 parent a0fadd0 commit 12b3d59

File tree

5 files changed

+35
-17
lines changed

5 files changed

+35
-17
lines changed

packages/google_sign_in/google_sign_in/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 6.2.1
2+
3+
* Improves README example and updates it to use code excerpts.
4+
15
## 6.2.0
26

37
* Adds support for macOS.

packages/google_sign_in/google_sign_in/README.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,27 @@ To use this plugin, follow the
6868

6969
### Use the plugin
7070

71-
Add the following import to your Dart code:
72-
73-
```dart
74-
import 'package:google_sign_in/google_sign_in.dart';
75-
```
76-
7771
Initialize `GoogleSignIn` with the scopes you want:
7872

73+
<?code-excerpt "example/lib/main.dart (Initialize)"?>
7974
```dart
75+
const List<String> scopes = <String>[
76+
'email',
77+
'https://www.googleapis.com/auth/contacts.readonly',
78+
];
79+
8080
GoogleSignIn _googleSignIn = GoogleSignIn(
81-
scopes: [
82-
'email',
83-
'https://www.googleapis.com/auth/contacts.readonly',
84-
],
81+
// Optional clientId
82+
// clientId: 'your-client_id.apps.googleusercontent.com',
83+
scopes: scopes,
8584
);
8685
```
8786

8887
[Full list of available scopes](https://developers.google.com/identity/protocols/googlescopes).
8988

9089
You can now use the `GoogleSignIn` class to authenticate in your Dart code, e.g.
9190

91+
<?code-excerpt "example/lib/main.dart (SignIn)"?>
9292
```dart
9393
Future<void> _handleSignIn() async {
9494
try {
@@ -122,8 +122,14 @@ Applications must be able to:
122122

123123
There's a new method that enables the checks above, `canAccessScopes`:
124124

125+
<?code-excerpt "example/lib/main.dart (CanAccessScopes)"?>
125126
```dart
126-
final bool isAuthorized = await _googleSignIn.canAccessScopes(scopes);
127+
// In mobile, being authenticated means being authorized...
128+
bool isAuthorized = account != null;
129+
// However, on web...
130+
if (kIsWeb && account != null) {
131+
isAuthorized = await _googleSignIn.canAccessScopes(scopes);
132+
}
127133
```
128134

129135
_(Only implemented in the web platform, from version 6.1.0 of this package)_
@@ -134,14 +140,13 @@ If an app determines that the user hasn't granted the scopes it requires, it
134140
should initiate an Authorization request. (Remember that in the web platform,
135141
this request **must be initiated from an user interaction**, like a button press).
136142

143+
<?code-excerpt "example/lib/main.dart (RequestScopes)" plaster="none"?>
137144
```dart
138145
Future<void> _handleAuthorizeScopes() async {
139146
final bool isAuthorized = await _googleSignIn.requestScopes(scopes);
140147
if (isAuthorized) {
141-
// Do things that only authorized users can do!
142-
_handleGetContact(_currentUser!);
148+
unawaited(_handleGetContact(_currentUser!));
143149
}
144-
}
145150
```
146151

147152
The `requestScopes` returns a `boolean` value that is `true` if the user has

packages/google_sign_in/google_sign_in/example/lib/main.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'package:http/http.dart' as http;
1515
import 'src/sign_in_button.dart';
1616

1717
/// The scopes required by this application.
18+
// #docregion Initialize
1819
const List<String> scopes = <String>[
1920
'email',
2021
'https://www.googleapis.com/auth/contacts.readonly',
@@ -25,6 +26,7 @@ GoogleSignIn _googleSignIn = GoogleSignIn(
2526
// clientId: 'your-client_id.apps.googleusercontent.com',
2627
scopes: scopes,
2728
);
29+
// #enddocregion Initialize
2830

2931
void main() {
3032
runApp(
@@ -55,12 +57,14 @@ class _SignInDemoState extends State<SignInDemo> {
5557

5658
_googleSignIn.onCurrentUserChanged
5759
.listen((GoogleSignInAccount? account) async {
60+
// #docregion CanAccessScopes
5861
// In mobile, being authenticated means being authorized...
5962
bool isAuthorized = account != null;
60-
// However, in the web...
63+
// However, on web...
6164
if (kIsWeb && account != null) {
6265
isAuthorized = await _googleSignIn.canAccessScopes(scopes);
6366
}
67+
// #enddocregion CanAccessScopes
6468

6569
setState(() {
6670
_currentUser = account;
@@ -136,28 +140,34 @@ class _SignInDemoState extends State<SignInDemo> {
136140
//
137141
// On the web, the on-click handler of the Sign In button is owned by the JS
138142
// SDK, so this method can be considered mobile only.
143+
// #docregion SignIn
139144
Future<void> _handleSignIn() async {
140145
try {
141146
await _googleSignIn.signIn();
142147
} catch (error) {
143148
print(error);
144149
}
145150
}
151+
// #enddocregion SignIn
146152

147153
// Prompts the user to authorize `scopes`.
148154
//
149155
// This action is **required** in platforms that don't perform Authentication
150156
// and Authorization at the same time (like the web).
151157
//
152158
// On the web, this must be called from an user interaction (button click).
159+
// #docregion RequestScopes
153160
Future<void> _handleAuthorizeScopes() async {
154161
final bool isAuthorized = await _googleSignIn.requestScopes(scopes);
162+
// #enddocregion RequestScopes
155163
setState(() {
156164
_isAuthorized = isAuthorized;
157165
});
166+
// #docregion RequestScopes
158167
if (isAuthorized) {
159168
unawaited(_handleGetContact(_currentUser!));
160169
}
170+
// #enddocregion RequestScopes
161171
}
162172

163173
Future<void> _handleSignOut() => _googleSignIn.disconnect();

packages/google_sign_in/google_sign_in/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for Google Sign-In, a secure authentication system
33
for signing in with a Google account.
44
repository: https://github.com/flutter/packages/tree/main/packages/google_sign_in/google_sign_in
55
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+google_sign_in%22
6-
version: 6.2.0
6+
version: 6.2.1
77

88
environment:
99
sdk: ">=3.2.0 <4.0.0"

script/configs/temp_exclude_excerpt.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
- espresso
99
- extension_google_sign_in_as_googleapis_auth
1010
- go_router_builder
11-
- google_sign_in/google_sign_in
1211
- image_picker_for_web
1312
- in_app_purchase/in_app_purchase
1413
- ios_platform_images

0 commit comments

Comments
 (0)