Skip to content

Commit a855133

Browse files
authored
[google_sign_in] Adds support to send clientId as a parameter (flutter#3640)
1 parent d443a59 commit a855133

File tree

6 files changed

+53
-7
lines changed

6 files changed

+53
-7
lines changed

packages/google_sign_in/google_sign_in/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 5.0.1
2+
3+
* Update platforms `init` function to prioritize `clientId` property when available;
4+
* Updates `google_sign_in_platform_interface` version.
5+
16
## 5.0.0
27

38
* Migrate to null safety.

packages/google_sign_in/google_sign_in/android/src/main/java/io/flutter/plugins/googlesignin/GoogleSignInPlugin.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ public void onMethodCall(MethodCall call, Result result) {
136136
String signInOption = call.argument("signInOption");
137137
List<String> requestedScopes = call.argument("scopes");
138138
String hostedDomain = call.argument("hostedDomain");
139-
delegate.init(result, signInOption, requestedScopes, hostedDomain);
139+
String clientId = call.argument("clientId");
140+
delegate.init(result, signInOption, requestedScopes, hostedDomain, clientId);
140141
break;
141142

142143
case METHOD_SIGN_IN_SILENTLY:
@@ -188,7 +189,11 @@ public void onMethodCall(MethodCall call, Result result) {
188189
public interface IDelegate {
189190
/** Initializes this delegate so that it is ready to perform other operations. */
190191
public void init(
191-
Result result, String signInOption, List<String> requestedScopes, String hostedDomain);
192+
Result result,
193+
String signInOption,
194+
List<String> requestedScopes,
195+
String hostedDomain,
196+
String clientId);
192197

193198
/**
194199
* Returns the account information for the user who is signed in to this app. If no user is
@@ -309,7 +314,11 @@ private void checkAndSetPendingOperation(String method, Result result, Object da
309314
*/
310315
@Override
311316
public void init(
312-
Result result, String signInOption, List<String> requestedScopes, String hostedDomain) {
317+
Result result,
318+
String signInOption,
319+
List<String> requestedScopes,
320+
String hostedDomain,
321+
String clientId) {
313322
try {
314323
GoogleSignInOptions.Builder optionsBuilder;
315324

@@ -334,7 +343,10 @@ public void init(
334343
context
335344
.getResources()
336345
.getIdentifier("default_web_client_id", "string", context.getPackageName());
337-
if (clientIdIdentifier != 0) {
346+
if (!Strings.isNullOrEmpty(clientId)) {
347+
optionsBuilder.requestIdToken(clientId);
348+
optionsBuilder.requestServerAuthCode(clientId);
349+
} else if (clientIdIdentifier != 0) {
338350
optionsBuilder.requestIdToken(context.getString(clientIdIdentifier));
339351
optionsBuilder.requestServerAuthCode(context.getString(clientIdIdentifier));
340352
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import 'package:flutter/material.dart';
1212
import 'package:google_sign_in/google_sign_in.dart';
1313

1414
GoogleSignIn _googleSignIn = GoogleSignIn(
15+
// Optional clientId
16+
// clientId: '479882132969-9i9aqik3jfjd7qhci1nqf0bm2g71rm1u.apps.googleusercontent.com',
1517
scopes: <String>[
1618
'email',
1719
'https://www.googleapis.com/auth/contacts.readonly',

packages/google_sign_in/google_sign_in/ios/Classes/FLTGoogleSignInPlugin.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,15 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
7777
ofType:@"plist"];
7878
if (path) {
7979
NSMutableDictionary *plist = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
80-
[GIDSignIn sharedInstance].clientID = plist[kClientIdKey];
80+
BOOL hasDynamicClientId =
81+
[[call.arguments valueForKey:@"clientId"] isKindOfClass:[NSString class]];
82+
83+
if (hasDynamicClientId) {
84+
[GIDSignIn sharedInstance].clientID = [call.arguments valueForKey:@"clientId"];
85+
} else {
86+
[GIDSignIn sharedInstance].clientID = plist[kClientIdKey];
87+
}
88+
8189
[GIDSignIn sharedInstance].serverClientID = plist[kServerClientIdKey];
8290
[GIDSignIn sharedInstance].scopes = call.arguments[@"scopes"];
8391
[GIDSignIn sharedInstance].hostedDomain = call.arguments[@"hostedDomain"];

packages/google_sign_in/google_sign_in/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: google_sign_in
22
description: Flutter plugin for Google Sign-In, a secure authentication system
33
for signing in with a Google account on Android and iOS.
44
homepage: https://github.com/flutter/plugins/tree/master/packages/google_sign_in/google_sign_in
5-
version: 5.0.0
5+
version: 5.0.1
66

77
flutter:
88
plugin:
@@ -16,7 +16,7 @@ flutter:
1616
default_package: google_sign_in_web
1717

1818
dependencies:
19-
google_sign_in_platform_interface: ^2.0.0
19+
google_sign_in_platform_interface: ^2.0.1
2020
google_sign_in_web: ^0.10.0
2121
flutter:
2222
sdk: flutter

packages/google_sign_in/google_sign_in/test/google_sign_in_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,25 @@ void main() {
8282
);
8383
});
8484

85+
test('signIn prioritize clientId parameter when available', () async {
86+
final fakeClientId = 'fakeClientId';
87+
googleSignIn = GoogleSignIn(clientId: fakeClientId);
88+
await googleSignIn.signIn();
89+
expect(googleSignIn.currentUser, isNotNull);
90+
expect(
91+
log,
92+
<Matcher>[
93+
isMethodCall('init', arguments: <String, dynamic>{
94+
'signInOption': 'SignInOption.standard',
95+
'scopes': <String>[],
96+
'hostedDomain': null,
97+
'clientId': fakeClientId,
98+
}),
99+
isMethodCall('signIn', arguments: null),
100+
],
101+
);
102+
});
103+
85104
test('signOut', () async {
86105
await googleSignIn.signOut();
87106
expect(googleSignIn.currentUser, isNull);

0 commit comments

Comments
 (0)