Skip to content

Commit

Permalink
added firebase auth + login
Browse files Browse the repository at this point in the history
  • Loading branch information
mdanics committed Mar 17, 2018
1 parent 5d5ca1b commit f75513f
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 45 deletions.
1 change: 0 additions & 1 deletion firestore_test.iml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
<excludeFolder url="file://$MODULE_DIR$/cloud_firestore/packages" />
<excludeFolder url="file://$MODULE_DIR$/cloud_firestore/test/packages" />
<excludeFolder url="file://$MODULE_DIR$/packages" />
<excludeFolder url="file://$MODULE_DIR$/test/packages" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Dart SDK" level="project" />
Expand Down
155 changes: 111 additions & 44 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -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<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());

Expand All @@ -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'),
);
}
}
Expand All @@ -38,52 +75,82 @@ class _HomePageState extends State<HomePage> {
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: <BottomNavigationBarItem>[
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: <Widget>[
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: <BottomNavigationBarItem>[
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) {
Expand Down
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f75513f

Please sign in to comment.