mirror of
https://github.com/flutter/packages.git
synced 2025-06-28 13:47:29 +08:00
[google_sign_in] Adopt code excerpts in README (#5521)
Improves README example and updates it to use code excerpts. Part of [flutter/flutter#102679](https://github.com/flutter/flutter/issues/102679)
This commit is contained in:

committed by
GitHub

parent
a0fadd00c2
commit
12b3d59d18
@ -1,3 +1,7 @@
|
|||||||
|
## 6.2.1
|
||||||
|
|
||||||
|
* Improves README example and updates it to use code excerpts.
|
||||||
|
|
||||||
## 6.2.0
|
## 6.2.0
|
||||||
|
|
||||||
* Adds support for macOS.
|
* Adds support for macOS.
|
||||||
|
@ -68,20 +68,19 @@ To use this plugin, follow the
|
|||||||
|
|
||||||
### Use the plugin
|
### 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:
|
Initialize `GoogleSignIn` with the scopes you want:
|
||||||
|
|
||||||
|
<?code-excerpt "example/lib/main.dart (Initialize)"?>
|
||||||
```dart
|
```dart
|
||||||
|
const List<String> scopes = <String>[
|
||||||
|
'email',
|
||||||
|
'https://www.googleapis.com/auth/contacts.readonly',
|
||||||
|
];
|
||||||
|
|
||||||
GoogleSignIn _googleSignIn = GoogleSignIn(
|
GoogleSignIn _googleSignIn = GoogleSignIn(
|
||||||
scopes: [
|
// Optional clientId
|
||||||
'email',
|
// clientId: 'your-client_id.apps.googleusercontent.com',
|
||||||
'https://www.googleapis.com/auth/contacts.readonly',
|
scopes: scopes,
|
||||||
],
|
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -89,6 +88,7 @@ GoogleSignIn _googleSignIn = GoogleSignIn(
|
|||||||
|
|
||||||
You can now use the `GoogleSignIn` class to authenticate in your Dart code, e.g.
|
You can now use the `GoogleSignIn` class to authenticate in your Dart code, e.g.
|
||||||
|
|
||||||
|
<?code-excerpt "example/lib/main.dart (SignIn)"?>
|
||||||
```dart
|
```dart
|
||||||
Future<void> _handleSignIn() async {
|
Future<void> _handleSignIn() async {
|
||||||
try {
|
try {
|
||||||
@ -122,8 +122,14 @@ Applications must be able to:
|
|||||||
|
|
||||||
There's a new method that enables the checks above, `canAccessScopes`:
|
There's a new method that enables the checks above, `canAccessScopes`:
|
||||||
|
|
||||||
|
<?code-excerpt "example/lib/main.dart (CanAccessScopes)"?>
|
||||||
```dart
|
```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)_
|
_(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
|
|||||||
should initiate an Authorization request. (Remember that in the web platform,
|
should initiate an Authorization request. (Remember that in the web platform,
|
||||||
this request **must be initiated from an user interaction**, like a button press).
|
this request **must be initiated from an user interaction**, like a button press).
|
||||||
|
|
||||||
|
<?code-excerpt "example/lib/main.dart (RequestScopes)" plaster="none"?>
|
||||||
```dart
|
```dart
|
||||||
Future<void> _handleAuthorizeScopes() async {
|
Future<void> _handleAuthorizeScopes() async {
|
||||||
final bool isAuthorized = await _googleSignIn.requestScopes(scopes);
|
final bool isAuthorized = await _googleSignIn.requestScopes(scopes);
|
||||||
if (isAuthorized) {
|
if (isAuthorized) {
|
||||||
// Do things that only authorized users can do!
|
unawaited(_handleGetContact(_currentUser!));
|
||||||
_handleGetContact(_currentUser!);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
The `requestScopes` returns a `boolean` value that is `true` if the user has
|
The `requestScopes` returns a `boolean` value that is `true` if the user has
|
||||||
|
@ -15,6 +15,7 @@ import 'package:http/http.dart' as http;
|
|||||||
import 'src/sign_in_button.dart';
|
import 'src/sign_in_button.dart';
|
||||||
|
|
||||||
/// The scopes required by this application.
|
/// The scopes required by this application.
|
||||||
|
// #docregion Initialize
|
||||||
const List<String> scopes = <String>[
|
const List<String> scopes = <String>[
|
||||||
'email',
|
'email',
|
||||||
'https://www.googleapis.com/auth/contacts.readonly',
|
'https://www.googleapis.com/auth/contacts.readonly',
|
||||||
@ -25,6 +26,7 @@ GoogleSignIn _googleSignIn = GoogleSignIn(
|
|||||||
// clientId: 'your-client_id.apps.googleusercontent.com',
|
// clientId: 'your-client_id.apps.googleusercontent.com',
|
||||||
scopes: scopes,
|
scopes: scopes,
|
||||||
);
|
);
|
||||||
|
// #enddocregion Initialize
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(
|
runApp(
|
||||||
@ -55,12 +57,14 @@ class _SignInDemoState extends State<SignInDemo> {
|
|||||||
|
|
||||||
_googleSignIn.onCurrentUserChanged
|
_googleSignIn.onCurrentUserChanged
|
||||||
.listen((GoogleSignInAccount? account) async {
|
.listen((GoogleSignInAccount? account) async {
|
||||||
|
// #docregion CanAccessScopes
|
||||||
// In mobile, being authenticated means being authorized...
|
// In mobile, being authenticated means being authorized...
|
||||||
bool isAuthorized = account != null;
|
bool isAuthorized = account != null;
|
||||||
// However, in the web...
|
// However, on web...
|
||||||
if (kIsWeb && account != null) {
|
if (kIsWeb && account != null) {
|
||||||
isAuthorized = await _googleSignIn.canAccessScopes(scopes);
|
isAuthorized = await _googleSignIn.canAccessScopes(scopes);
|
||||||
}
|
}
|
||||||
|
// #enddocregion CanAccessScopes
|
||||||
|
|
||||||
setState(() {
|
setState(() {
|
||||||
_currentUser = account;
|
_currentUser = account;
|
||||||
@ -136,6 +140,7 @@ class _SignInDemoState extends State<SignInDemo> {
|
|||||||
//
|
//
|
||||||
// On the web, the on-click handler of the Sign In button is owned by the JS
|
// 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.
|
// SDK, so this method can be considered mobile only.
|
||||||
|
// #docregion SignIn
|
||||||
Future<void> _handleSignIn() async {
|
Future<void> _handleSignIn() async {
|
||||||
try {
|
try {
|
||||||
await _googleSignIn.signIn();
|
await _googleSignIn.signIn();
|
||||||
@ -143,6 +148,7 @@ class _SignInDemoState extends State<SignInDemo> {
|
|||||||
print(error);
|
print(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// #enddocregion SignIn
|
||||||
|
|
||||||
// Prompts the user to authorize `scopes`.
|
// Prompts the user to authorize `scopes`.
|
||||||
//
|
//
|
||||||
@ -150,14 +156,18 @@ class _SignInDemoState extends State<SignInDemo> {
|
|||||||
// and Authorization at the same time (like the web).
|
// and Authorization at the same time (like the web).
|
||||||
//
|
//
|
||||||
// On the web, this must be called from an user interaction (button click).
|
// On the web, this must be called from an user interaction (button click).
|
||||||
|
// #docregion RequestScopes
|
||||||
Future<void> _handleAuthorizeScopes() async {
|
Future<void> _handleAuthorizeScopes() async {
|
||||||
final bool isAuthorized = await _googleSignIn.requestScopes(scopes);
|
final bool isAuthorized = await _googleSignIn.requestScopes(scopes);
|
||||||
|
// #enddocregion RequestScopes
|
||||||
setState(() {
|
setState(() {
|
||||||
_isAuthorized = isAuthorized;
|
_isAuthorized = isAuthorized;
|
||||||
});
|
});
|
||||||
|
// #docregion RequestScopes
|
||||||
if (isAuthorized) {
|
if (isAuthorized) {
|
||||||
unawaited(_handleGetContact(_currentUser!));
|
unawaited(_handleGetContact(_currentUser!));
|
||||||
}
|
}
|
||||||
|
// #enddocregion RequestScopes
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _handleSignOut() => _googleSignIn.disconnect();
|
Future<void> _handleSignOut() => _googleSignIn.disconnect();
|
||||||
|
@ -3,7 +3,7 @@ description: Flutter plugin for Google Sign-In, a secure authentication system
|
|||||||
for signing in with a Google account.
|
for signing in with a Google account.
|
||||||
repository: https://github.com/flutter/packages/tree/main/packages/google_sign_in/google_sign_in
|
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
|
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:
|
environment:
|
||||||
sdk: ">=3.2.0 <4.0.0"
|
sdk: ">=3.2.0 <4.0.0"
|
||||||
|
@ -8,7 +8,6 @@
|
|||||||
- espresso
|
- espresso
|
||||||
- extension_google_sign_in_as_googleapis_auth
|
- extension_google_sign_in_as_googleapis_auth
|
||||||
- go_router_builder
|
- go_router_builder
|
||||||
- google_sign_in/google_sign_in
|
|
||||||
- image_picker_for_web
|
- image_picker_for_web
|
||||||
- in_app_purchase/in_app_purchase
|
- in_app_purchase/in_app_purchase
|
||||||
- ios_platform_images
|
- ios_platform_images
|
||||||
|
Reference in New Issue
Block a user