diff --git a/firestore_test.iml b/firestore_test.iml index dc2db41..5acb882 100644 --- a/firestore_test.iml +++ b/firestore_test.iml @@ -45,7 +45,6 @@ - diff --git a/lib/main.dart b/lib/main.dart index e0ca417..5f1d56d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,43 @@ import 'package:flutter/material.dart'; import 'feed.dart'; import 'upload_page.dart'; +import 'dart:async'; +import 'package:google_sign_in/google_sign_in.dart'; +import 'package:firebase_auth/firebase_auth.dart'; + +final auth = FirebaseAuth.instance; +final googleSignIn = new GoogleSignIn(); + +Future _ensureLoggedIn() async { + GoogleSignInAccount user = googleSignIn.currentUser; + if (user == null) { + user = await googleSignIn.signInSilently(); + } + if (user == null) { + await googleSignIn.signIn(); + } + + if (await auth.currentUser() == null) { + GoogleSignInAuthentication credentials = + await googleSignIn.currentUser.authentication; + await auth.signInWithGoogle( + idToken: credentials.idToken, accessToken: credentials.accessToken); + } +} + +Future _silentLogin() async { + GoogleSignInAccount user = googleSignIn.currentUser; + if (user == null) { + user = await googleSignIn.signInSilently(); + } + + if (await auth.currentUser() == null && user != null) { + GoogleSignInAuthentication credentials = + await googleSignIn.currentUser.authentication; + await auth.signInWithGoogle( + idToken: credentials.idToken, accessToken: credentials.accessToken); + } +} void main() => runApp(new MyApp()); @@ -21,7 +58,7 @@ class MyApp extends StatelessWidget { // counter didn't reset back to zero; the application is not restarted. primarySwatch: Colors.blue, ), - home: new HomePage(title: 'Flutter Demo Home Page'), + home: new HomePage(title: 'Fluttergram'), ); } } @@ -38,52 +75,82 @@ class _HomePageState extends State { PageController _pageController; int _page = 0; + bool triedSilentLogin = false; @override Widget build(BuildContext context) { - return new Scaffold( - body: new PageView( - children: [ - new Container( - color: Colors.white, - child: new Feed(), - ), - new Container(color: Colors.green,), - new Container(color: Colors.white, child: new Uploader()), - new Container(color: Colors.amber), - new Container(color: Colors.white, child: new PostForm()), - ], - controller: _pageController, - physics: new NeverScrollableScrollPhysics(), - onPageChanged: onPageChanged, - ), - bottomNavigationBar: new BottomNavigationBar( - items: [ - new BottomNavigationBarItem( - icon: new Icon(Icons.home, color: Colors.grey), - title: new Text(""), - backgroundColor: Colors.white), - new BottomNavigationBarItem( - icon: new Icon(Icons.search, color: Colors.grey), - title: new Text(""), - backgroundColor: Colors.white), - new BottomNavigationBarItem( - icon: new Icon(Icons.add_circle, color: Colors.grey), - title: new Text(""), - backgroundColor: Colors.white), - new BottomNavigationBarItem( - icon: new Icon(Icons.star, color: Colors.grey), - title: new Text(""), - backgroundColor: Colors.white), - new BottomNavigationBarItem( - icon: new Icon(Icons.person_outline, color: Colors.grey), - title: new Text(""), - backgroundColor: Colors.white), - ], - onTap: navigationTapped, - currentIndex: _page, - ), - ); + if (triedSilentLogin == false) { + silentLogin(); + } // might cause performance issues? + return googleSignIn.currentUser == null + ? new Container( + alignment: FractionalOffset.center, + width: 20.0, + child: new RaisedButton( + onPressed: login, + child: new Row(children: [ + new Icon(Icons.business), + new Text("Sign in with Google") + ]), + ), + ) + : new Scaffold( + body: new PageView( + children: [ + new Container( + color: Colors.white, + child: new Feed(), + ), + new Container( + color: Colors.green, + ), + new Container(color: Colors.white, child: new Uploader()), + new Container(color: Colors.amber), + new Container(color: Colors.white, child: new Text('Test')), + ], + controller: _pageController, + physics: new NeverScrollableScrollPhysics(), + onPageChanged: onPageChanged, + ), + bottomNavigationBar: new BottomNavigationBar( + items: [ + new BottomNavigationBarItem( + icon: new Icon(Icons.home, color: Colors.grey), + title: new Text(""), + backgroundColor: Colors.white), + new BottomNavigationBarItem( + icon: new Icon(Icons.search, color: Colors.grey), + title: new Text(""), + backgroundColor: Colors.white), + new BottomNavigationBarItem( + icon: new Icon(Icons.add_circle, color: Colors.grey), + title: new Text(""), + backgroundColor: Colors.white), + new BottomNavigationBarItem( + icon: new Icon(Icons.star, color: Colors.grey), + title: new Text(""), + backgroundColor: Colors.white), + new BottomNavigationBarItem( + icon: new Icon(Icons.person_outline, color: Colors.grey), + title: new Text(""), + backgroundColor: Colors.white), + ], + onTap: navigationTapped, + currentIndex: _page, + ), + ); + } + + void login() async { + await _ensureLoggedIn(); + setState(() { + triedSilentLogin = true; + }); + } + + void silentLogin() async { + await _silentLogin(); + setState(() {}); } void navigationTapped(int page) { diff --git a/pubspec.lock b/pubspec.lock index 86cb7b1..a4ab56f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -92,6 +92,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.1.1" + firebase_auth: + dependency: "direct main" + description: + name: firebase_auth + url: "https://pub.dartlang.org" + source: hosted + version: "0.5.0" firebase_storage: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index a123077..f410478 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,9 +7,11 @@ dependencies: cloud_firestore: 0.3.0 image_picker: 0.1.1 firebase_storage: 0.2.0 + firebase_auth: 0.5.0 google_sign_in: 3.0.0 uuid: 0.5.3 + # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^0.1.0