Skip to content

Commit 5f9577b

Browse files
fbcouchmauricioluz
authored andcommitted
[google_sign_in] Add forceCodeForRefreshToken parameter (and new SignInInitParameters class) (flutter#5325)
1 parent 797ccc0 commit 5f9577b

File tree

7 files changed

+90
-8
lines changed

7 files changed

+90
-8
lines changed

packages/google_sign_in/google_sign_in_platform_interface/AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,4 @@ Aleksandr Yurkovskiy <[email protected]>
6464
Anton Borries <[email protected]>
6565
6666
Rahul Raj <[email protected]>
67+
Twin Sun, LLC <[email protected]>

packages/google_sign_in/google_sign_in_platform_interface/CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
## NEXT
1+
## 2.1.3
22

33
* Removes unnecessary imports.
4+
* Adds `SignInInitParameters` class to hold all sign in params, including the new `forceCodeForRefreshToken`.
45

56
## 2.1.2
67

packages/google_sign_in/google_sign_in_platform_interface/lib/google_sign_in_platform_interface.dart

+16-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ abstract class GoogleSignInPlatform {
6363
/// if the provided instance is a class implemented with `implements`.
6464
void _verifyProvidesDefaultImplementations() {}
6565

66-
/// Initializes the plugin. You must call this method before calling other
67-
/// methods.
66+
/// Initializes the plugin. Deprecated: call [initWithParams] instead.
6867
///
6968
/// The [hostedDomain] argument specifies a hosted domain restriction. By
7069
/// setting this, sign in will be restricted to accounts of the user in the
@@ -89,6 +88,21 @@ abstract class GoogleSignInPlatform {
8988
throw UnimplementedError('init() has not been implemented.');
9089
}
9190

91+
/// Initializes the plugin with specified [params]. You must call this method
92+
/// before calling other methods.
93+
///
94+
/// See:
95+
///
96+
/// * [SignInInitParameters]
97+
Future<void> initWithParams(SignInInitParameters params) async {
98+
await init(
99+
scopes: params.scopes,
100+
signInOption: params.signInOption,
101+
hostedDomain: params.hostedDomain,
102+
clientId: params.clientId,
103+
);
104+
}
105+
92106
/// Attempts to reuse pre-existing credentials to sign in again, without user interaction.
93107
Future<GoogleSignInUserData?> signInSilently() async {
94108
throw UnimplementedError('signInSilently() has not been implemented.');

packages/google_sign_in/google_sign_in_platform_interface/lib/src/method_channel_google_sign_in.dart

+14-4
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,21 @@ class MethodChannelGoogleSignIn extends GoogleSignInPlatform {
2525
String? hostedDomain,
2626
String? clientId,
2727
}) {
28+
return initWithParams(SignInInitParameters(
29+
scopes: scopes,
30+
signInOption: signInOption,
31+
hostedDomain: hostedDomain,
32+
clientId: clientId));
33+
}
34+
35+
@override
36+
Future<void> initWithParams(SignInInitParameters params) {
2837
return channel.invokeMethod<void>('init', <String, dynamic>{
29-
'signInOption': signInOption.toString(),
30-
'scopes': scopes,
31-
'hostedDomain': hostedDomain,
32-
'clientId': clientId,
38+
'signInOption': params.signInOption.toString(),
39+
'scopes': params.scopes,
40+
'hostedDomain': params.hostedDomain,
41+
'clientId': params.clientId,
42+
'forceCodeForRefreshToken': params.forceCodeForRefreshToken,
3343
});
3444
}
3545

packages/google_sign_in/google_sign_in_platform_interface/lib/src/types.dart

+37
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:flutter/widgets.dart';
56
import 'package:quiver/core.dart';
67

78
/// Default configuration options to use when signing in.
@@ -22,6 +23,42 @@ enum SignInOption {
2223
games
2324
}
2425

26+
/// The parameters to use when initializing the sign in process.
27+
///
28+
/// See:
29+
/// https://developers.google.com/identity/sign-in/web/reference#gapiauth2initparams
30+
@immutable
31+
class SignInInitParameters {
32+
/// The parameters to use when initializing the sign in process.
33+
const SignInInitParameters({
34+
this.scopes = const <String>[],
35+
this.signInOption = SignInOption.standard,
36+
this.hostedDomain,
37+
this.clientId,
38+
this.forceCodeForRefreshToken = false,
39+
});
40+
41+
/// The list of OAuth scope codes to request when signing in.
42+
final List<String> scopes;
43+
44+
/// The user experience to use when signing in. [SignInOption.games] is
45+
/// only supported on Android.
46+
final SignInOption signInOption;
47+
48+
/// Restricts sign in to accounts of the user in the specified domain.
49+
/// By default, the list of accounts will not be restricted.
50+
final String? hostedDomain;
51+
52+
/// The client ID to use when signing in.
53+
final String? clientId;
54+
55+
/// If true, ensures the authorization code can be exchanged for an access
56+
/// token.
57+
///
58+
/// This is only used on Android.
59+
final bool forceCodeForRefreshToken;
60+
}
61+
2562
/// Holds information about the signed in user.
2663
class GoogleSignInUserData {
2764
/// Uses the given data to construct an instance.

packages/google_sign_in/google_sign_in_platform_interface/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/main/packages/google_sign_in
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+google_sign_in%22
55
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
66
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7-
version: 2.1.2
7+
version: 2.1.3
88

99
environment:
1010
sdk: ">=2.12.0 <3.0.0"

packages/google_sign_in/google_sign_in_platform_interface/test/method_channel_google_sign_in_test.dart

+19
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ void main() {
107107
'scopes': <String>['two', 'scopes'],
108108
'signInOption': 'SignInOption.games',
109109
'clientId': 'fakeClientId',
110+
'forceCodeForRefreshToken': false,
110111
}),
111112
() {
112113
googleSignIn.getTokens(
@@ -136,5 +137,23 @@ void main() {
136137

137138
expect(log, tests.values);
138139
});
140+
141+
test('initWithParams passes through arguments to the channel', () async {
142+
await googleSignIn.initWithParams(const SignInInitParameters(
143+
hostedDomain: 'example.com',
144+
scopes: <String>['two', 'scopes'],
145+
signInOption: SignInOption.games,
146+
clientId: 'fakeClientId',
147+
forceCodeForRefreshToken: true));
148+
expect(log, <Matcher>[
149+
isMethodCall('init', arguments: <String, dynamic>{
150+
'hostedDomain': 'example.com',
151+
'scopes': <String>['two', 'scopes'],
152+
'signInOption': 'SignInOption.games',
153+
'clientId': 'fakeClientId',
154+
'forceCodeForRefreshToken': true,
155+
}),
156+
]);
157+
});
139158
});
140159
}

0 commit comments

Comments
 (0)