-
Notifications
You must be signed in to change notification settings - Fork 100
Switch to federated google sign in plugin #525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0801c74
4c58d8b
8a13df8
858a027
91a7413
970cd84
6132567
0264df2
d8a2c4c
3be9a9f
237a7fe
2c03377
6fdfc16
f85ddef
7acbb86
bde0114
a99f5f4
55445c8
b60179c
f0fd559
f98470f
20f88a7
05b3248
8a3c957
970ba1f
34cb34c
095a5ed
5b79930
e9b0fd2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,18 +2,27 @@ | |
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:google_sign_in_all/google_sign_in_all.dart'; | ||
| import 'package:flutter/rendering.dart'; | ||
| import 'package:google_sign_in/google_sign_in.dart'; | ||
|
|
||
| /// Service class for interacting with Google Sign In authentication for Cocoon backend. | ||
| class GoogleSignInService { | ||
| /// Creates a new [GoogleSignIn]. | ||
| GoogleSignInService({GoogleSignIn googleSignIn}) | ||
| GoogleSignInService({GoogleSignIn googleSignIn, this.notifyListeners}) | ||
| : _googleSignIn = googleSignIn ?? | ||
| setupGoogleSignIn( | ||
| GoogleSignIn( | ||
| scopes: _googleScopes, | ||
| webClientId: | ||
| '308150028417-vlj9mqlm3gk1d03fb0efif1fu5nagdtt.apps.googleusercontent.com', | ||
| ); | ||
| ) { | ||
| _googleSignIn.onCurrentUserChanged | ||
| .listen((GoogleSignInAccount accountValue) { | ||
| user = accountValue; | ||
| notifyListeners(); | ||
| }); | ||
| _googleSignIn.signInSilently(); | ||
| } | ||
|
|
||
| /// A callback for notifying listeners there has been an update. | ||
| final VoidCallback notifyListeners; | ||
|
|
||
| /// A list of Google API OAuth Scopes this project needs access to. | ||
| /// | ||
|
|
@@ -24,30 +33,29 @@ class GoogleSignInService { | |
| static const List<String> _googleScopes = <String>[ | ||
| 'https://www.googleapis.com/auth/userinfo.email', | ||
| 'https://www.googleapis.com/auth/userinfo.profile', | ||
| 'openid', | ||
| ]; | ||
|
|
||
| // TODO(chillers): Switch to official Flutter plugin when it supports web. | ||
| final GoogleSignIn _googleSignIn; | ||
|
|
||
| AuthCredentials _credentials; | ||
|
|
||
| GoogleAccount _user; | ||
|
|
||
| /// Whether or not the application has been signed in to. | ||
| bool get isAuthenticated => _credentials?.accessToken != null; | ||
| Future<bool> get isAuthenticated => _googleSignIn.isSignedIn(); | ||
|
|
||
| /// The profile photo url of the current user signed in. | ||
| String get avatarUrl => _user?.photoUrl; | ||
|
|
||
| /// The email of the current user signed in. | ||
| String get email => _user?.email; | ||
| /// The Google Account for the signed in user, null if no user is signed in. | ||
| /// | ||
| /// Read only object with only access to clear client auth tokens. | ||
| GoogleSignInAccount user; | ||
|
|
||
| /// Authentication token to be sent to Cocoon Backend to verify API calls. | ||
| String get accessToken => _credentials?.accessToken; | ||
| Future<String> get idToken => user?.authentication | ||
| ?.then((GoogleSignInAuthentication key) => key.idToken); | ||
|
|
||
| /// Initiate the Google Sign In process. | ||
| Future<void> signIn() async { | ||
| _credentials = await _googleSignIn.signIn(); | ||
| _user = await _googleSignIn.getCurrentUser(); | ||
| user = await _googleSignIn.signIn(); | ||
| } | ||
|
|
||
| Future<void> signOut() async { | ||
| await _googleSignIn.signOut(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No action required, just want to mention that it's amusing to see "SignIn.signOut" :) |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Copyright (c) 2019 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter/widgets.dart'; | ||
|
|
||
| import 'service/google_authentication.dart'; | ||
|
|
||
| /// Widget for displaying sign in information for the current user. | ||
| /// | ||
| /// If logged in, it will display the user's avatar. Clicking it opens a dropdown for logging out. | ||
| /// Otherwise, a sign in button will show. | ||
| class SignInButton extends StatelessWidget { | ||
| const SignInButton({@required this.authService, Key key}) : super(key: key); | ||
|
|
||
| final GoogleSignInService authService; | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return FutureBuilder<bool>( | ||
| future: authService.isAuthenticated, | ||
| builder: (_, AsyncSnapshot<bool> isAuthenticated) { | ||
| /// On sign out, there's a second where the user is null before isAuthenticated catches up. | ||
| if (isAuthenticated.data == true && authService.user != null) { | ||
| return PopupMenuButton<String>( | ||
| // TODO(chillers): Switch to use avatar widget provided by google_sign_in plugin | ||
| child: Image.network(authService.user?.photoUrl), | ||
| offset: const Offset(0, 50), | ||
| itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[ | ||
| const PopupMenuItem<String>( | ||
| value: 'logout', | ||
| child: Text('Log out'), | ||
| ), | ||
| ], | ||
| onSelected: (String value) { | ||
| if (value == 'logout') { | ||
| authService.signOut(); | ||
| } | ||
| }, | ||
| ); | ||
| } | ||
| return FlatButton( | ||
| child: const Text( | ||
| 'Sign in', | ||
| style: TextStyle(color: Colors.white), | ||
| ), | ||
| onPressed: () => authService.signIn(), | ||
| ); | ||
| }, | ||
| ); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,7 @@ description: A new Flutter project. | |
| version: 1.0.0+1 | ||
|
|
||
| environment: | ||
| sdk: ">=2.3.0 <3.0.0" | ||
| sdk: ">=2.6.0 <3.0.0" | ||
|
|
||
| dependencies: | ||
| flutter: | ||
|
|
@@ -31,7 +31,14 @@ dependencies: | |
| cocoon_service: | ||
| path: ../app_dart | ||
| flutter_progress_button: ^1.0.0 | ||
| google_sign_in_all: ^0.0.3 | ||
| google_sign_in: ^4.0.14 | ||
| google_sign_in_web: | ||
| git: | ||
| # TODO(chillers): https://github.com/flutter/plugins/pull/2280 and the Google Sign In plugin | ||
| # publishes the update switch to the official version. | ||
| url: git://github.com/ditman/plugins.git | ||
| ref: federated_google_sign_in_web | ||
| path: packages/google_sign_in/google_sign_in_web | ||
|
Comment on lines
+39
to
+41
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had no idea you could point to a repo+branch when using the git source. Nice! |
||
| provider: ^3.0.0 | ||
| url_launcher: 5.2.4 | ||
| url_launcher_web: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't want to change the getter signature you may be able to do:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(You might need to change it to an async fn, so maybe you still need to change the signature after all, hmmm)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.