added firebase auth + login

This commit is contained in:
Matthew
2018-03-17 10:09:55 +02:00
parent 5d5ca1be6f
commit f75513f18c
4 changed files with 120 additions and 45 deletions

View File

@ -45,7 +45,6 @@
<excludeFolder url="file://$MODULE_DIR$/cloud_firestore/packages" /> <excludeFolder url="file://$MODULE_DIR$/cloud_firestore/packages" />
<excludeFolder url="file://$MODULE_DIR$/cloud_firestore/test/packages" /> <excludeFolder url="file://$MODULE_DIR$/cloud_firestore/test/packages" />
<excludeFolder url="file://$MODULE_DIR$/packages" /> <excludeFolder url="file://$MODULE_DIR$/packages" />
<excludeFolder url="file://$MODULE_DIR$/test/packages" />
</content> </content>
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Dart SDK" level="project" /> <orderEntry type="library" name="Dart SDK" level="project" />

View File

@ -1,6 +1,43 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'feed.dart'; import 'feed.dart';
import 'upload_page.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<Null> _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<Null> _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()); 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. // counter didn't reset back to zero; the application is not restarted.
primarySwatch: Colors.blue, primarySwatch: Colors.blue,
), ),
home: new HomePage(title: 'Flutter Demo Home Page'), home: new HomePage(title: 'Fluttergram'),
); );
} }
} }
@ -38,52 +75,82 @@ class _HomePageState extends State<HomePage> {
PageController _pageController; PageController _pageController;
int _page = 0; int _page = 0;
bool triedSilentLogin = false;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return new Scaffold( if (triedSilentLogin == false) {
body: new PageView( silentLogin();
children: [ } // might cause performance issues?
new Container( return googleSignIn.currentUser == null
color: Colors.white, ? new Container(
child: new Feed(), alignment: FractionalOffset.center,
), width: 20.0,
new Container(color: Colors.green,), child: new RaisedButton(
new Container(color: Colors.white, child: new Uploader()), onPressed: login,
new Container(color: Colors.amber), child: new Row(children: <Widget>[
new Container(color: Colors.white, child: new PostForm()), new Icon(Icons.business),
], new Text("Sign in with Google")
controller: _pageController, ]),
physics: new NeverScrollableScrollPhysics(), ),
onPageChanged: onPageChanged, )
), : new Scaffold(
bottomNavigationBar: new BottomNavigationBar( body: new PageView(
items: <BottomNavigationBarItem>[ children: [
new BottomNavigationBarItem( new Container(
icon: new Icon(Icons.home, color: Colors.grey), color: Colors.white,
title: new Text(""), child: new Feed(),
backgroundColor: Colors.white), ),
new BottomNavigationBarItem( new Container(
icon: new Icon(Icons.search, color: Colors.grey), color: Colors.green,
title: new Text(""), ),
backgroundColor: Colors.white), new Container(color: Colors.white, child: new Uploader()),
new BottomNavigationBarItem( new Container(color: Colors.amber),
icon: new Icon(Icons.add_circle, color: Colors.grey), new Container(color: Colors.white, child: new Text('Test')),
title: new Text(""), ],
backgroundColor: Colors.white), controller: _pageController,
new BottomNavigationBarItem( physics: new NeverScrollableScrollPhysics(),
icon: new Icon(Icons.star, color: Colors.grey), onPageChanged: onPageChanged,
title: new Text(""), ),
backgroundColor: Colors.white), bottomNavigationBar: new BottomNavigationBar(
new BottomNavigationBarItem( items: <BottomNavigationBarItem>[
icon: new Icon(Icons.person_outline, color: Colors.grey), new BottomNavigationBarItem(
title: new Text(""), icon: new Icon(Icons.home, color: Colors.grey),
backgroundColor: Colors.white), title: new Text(""),
], backgroundColor: Colors.white),
onTap: navigationTapped, new BottomNavigationBarItem(
currentIndex: _page, 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) { void navigationTapped(int page) {

View File

@ -92,6 +92,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.1.1" 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: firebase_storage:
dependency: "direct main" dependency: "direct main"
description: description:

View File

@ -7,9 +7,11 @@ dependencies:
cloud_firestore: 0.3.0 cloud_firestore: 0.3.0
image_picker: 0.1.1 image_picker: 0.1.1
firebase_storage: 0.2.0 firebase_storage: 0.2.0
firebase_auth: 0.5.0
google_sign_in: 3.0.0 google_sign_in: 3.0.0
uuid: 0.5.3 uuid: 0.5.3
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.0 cupertino_icons: ^0.1.0