Skip to content
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
4 changes: 4 additions & 0 deletions packages/google_sign_in/google_sign_in/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 6.2.1

* Improves README example and updates it to use code excerpts.

## 6.2.0

* Adds support for macOS.
Expand Down
33 changes: 19 additions & 14 deletions packages/google_sign_in/google_sign_in/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,27 @@ 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:

<?code-excerpt "example/lib/main.dart (Initialize)"?>
```dart
const List<String> scopes = <String>[
'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,
);
```

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

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

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

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

<?code-excerpt "example/lib/main.dart (CanAccessScopes)"?>
```dart
final bool isAuthorized = await _googleSignIn.canAccessScopes(scopes);
// In mobile, being authenticated means being authorized...
bool isAuthorized = account != null;
// However, on web...
if (kIsWeb && account != null) {
isAuthorized = await _googleSignIn.canAccessScopes(scopes);
}
```

_(Only implemented in the web platform, from version 6.1.0 of this package)_
Expand All @@ -134,14 +140,13 @@ 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).

<?code-excerpt "example/lib/main.dart (RequestScopes)" plaster="none"?>
```dart
Future<void> _handleAuthorizeScopes() async {
final bool isAuthorized = await _googleSignIn.requestScopes(scopes);
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
Expand Down
12 changes: 11 additions & 1 deletion packages/google_sign_in/google_sign_in/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:http/http.dart' as http;
import 'src/sign_in_button.dart';

/// The scopes required by this application.
// #docregion Initialize
const List<String> scopes = <String>[
'email',
'https://www.googleapis.com/auth/contacts.readonly',
Expand All @@ -25,6 +26,7 @@ GoogleSignIn _googleSignIn = GoogleSignIn(
// clientId: 'your-client_id.apps.googleusercontent.com',
scopes: scopes,
);
// #enddocregion Initialize

void main() {
runApp(
Expand Down Expand Up @@ -55,12 +57,14 @@ class _SignInDemoState extends State<SignInDemo> {

_googleSignIn.onCurrentUserChanged
.listen((GoogleSignInAccount? account) async {
// #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);
}
// #enddocregion CanAccessScopes

setState(() {
_currentUser = account;
Expand Down Expand Up @@ -136,28 +140,34 @@ class _SignInDemoState extends State<SignInDemo> {
//
// 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<void> _handleSignIn() async {
try {
await _googleSignIn.signIn();
} catch (error) {
print(error);
}
}
// #enddocregion SignIn

// Prompts the user to authorize `scopes`.
//
// This action is **required** in platforms that don't perform Authentication
// 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<void> _handleAuthorizeScopes() async {
final bool isAuthorized = await _googleSignIn.requestScopes(scopes);
// #enddocregion RequestScopes
setState(() {
_isAuthorized = isAuthorized;
});
// #docregion RequestScopes
if (isAuthorized) {
unawaited(_handleGetContact(_currentUser!));
}
// #enddocregion RequestScopes
}

Future<void> _handleSignOut() => _googleSignIn.disconnect();
Expand Down
2 changes: 1 addition & 1 deletion packages/google_sign_in/google_sign_in/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for Google Sign-In, a secure authentication system
for signing in with a Google account.
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.2.0
version: 6.2.1

environment:
sdk: ">=3.2.0 <4.0.0"
Expand Down
1 change: 0 additions & 1 deletion script/configs/temp_exclude_excerpt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
- espresso
- extension_google_sign_in_as_googleapis_auth
- go_router_builder
- google_sign_in/google_sign_in
- image_picker_for_web
- in_app_purchase/in_app_purchase
- ios_platform_images
Expand Down