Skip to content

Commit c6e7888

Browse files
ditmanHarry Terkelsen
andauthored
[google_sign_in_web] Ensure not-signed-in users are returned as null. (flutter#2649)
The core plugin assumes that a not-authenticated user is being returned as null from the platform code, but the web implementation is currently returning a null-object (as in the design pattern). This change ensures that not signed-in users are returned as null, not as an object with all its properties set to null! Co-Authored-By: Harry Terkelsen <[email protected]>
1 parent 6b6be74 commit c6e7888

File tree

5 files changed

+61
-3
lines changed

5 files changed

+61
-3
lines changed

packages/google_sign_in/google_sign_in_web/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.9.1
2+
3+
* Ensure the web code returns `null` when the user is not signed in, instead of a `null-object` User. Fixes [issue 52338](https://github.com/flutter/flutter/issues/52338).
4+
15
## 0.9.0
26

37
* Add support for methods introduced in `google_sign_in_platform_interface` 1.1.0.

packages/google_sign_in/google_sign_in_web/lib/src/utils.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ Future<void> injectJSLibraries(List<String> libraries,
3131

3232
/// Utility method that converts `currentUser` to the equivalent
3333
/// [GoogleSignInUserData].
34+
/// This method returns `null` when the [currentUser] is not signed in.
3435
GoogleSignInUserData gapiUserToPluginUserData(auth2.GoogleUser currentUser) {
35-
assert(currentUser != null);
36-
final auth2.BasicProfile profile = currentUser.getBasicProfile();
36+
final bool isSignedIn = currentUser?.isSignedIn() ?? false;
37+
final auth2.BasicProfile profile = currentUser?.getBasicProfile();
38+
if (!isSignedIn || profile?.getId() == null) {
39+
return null;
40+
}
3741
return GoogleSignInUserData(
3842
displayName: profile?.getName(),
3943
email: profile?.getEmail(),

packages/google_sign_in/google_sign_in_web/pubspec.yaml

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

77
flutter:
88
plugin:
@@ -25,6 +25,7 @@ dev_dependencies:
2525
sdk: flutter
2626
google_sign_in: ^4.0.14
2727
pedantic: ^1.8.0
28+
mockito: ^4.1.1
2829

2930
environment:
3031
sdk: ">=2.6.0 <3.0.0"

packages/google_sign_in/google_sign_in_web/test/gapi_mocks/src/google_user.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@ String googleUser(GoogleSignInUserData data) => '''
2323
},
2424
getGrantedScopes: () => 'some scope',
2525
grant: () => true,
26+
isSignedIn: () => {
27+
return ${data != null ? 'true' : 'false'};
28+
},
2629
}
2730
''';
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
@TestOn('browser')
5+
6+
import 'package:flutter_test/flutter_test.dart';
7+
8+
import 'package:google_sign_in_web/src/generated/gapiauth2.dart' as gapi;
9+
import 'package:google_sign_in_web/src/utils.dart';
10+
import 'package:mockito/mockito.dart';
11+
12+
class MockGoogleUser extends Mock implements gapi.GoogleUser {}
13+
14+
class MockBasicProfile extends Mock implements gapi.BasicProfile {}
15+
16+
void main() {
17+
// The non-null use cases are covered by the auth2_test.dart file.
18+
19+
group('gapiUserToPluginUserData', () {
20+
var mockUser;
21+
22+
setUp(() {
23+
mockUser = MockGoogleUser();
24+
});
25+
26+
test('null user -> null response', () {
27+
expect(gapiUserToPluginUserData(null), isNull);
28+
});
29+
30+
test('not signed-in user -> null response', () {
31+
when(mockUser.isSignedIn()).thenReturn(false);
32+
expect(gapiUserToPluginUserData(mockUser), isNull);
33+
});
34+
35+
test('signed-in, but null profile user -> null response', () {
36+
when(mockUser.isSignedIn()).thenReturn(true);
37+
expect(gapiUserToPluginUserData(mockUser), isNull);
38+
});
39+
40+
test('signed-in, null userId in profile user -> null response', () {
41+
when(mockUser.isSignedIn()).thenReturn(true);
42+
when(mockUser.getBasicProfile()).thenReturn(MockBasicProfile());
43+
expect(gapiUserToPluginUserData(mockUser), isNull);
44+
});
45+
});
46+
}

0 commit comments

Comments
 (0)