mirror of
https://github.com/mdanics/fluttergram.git
synced 2025-08-06 13:19:53 +08:00
added firebase auth + login
This commit is contained in:
@ -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" />
|
||||
|
155
lib/main.dart
155
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<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());
|
||||
|
||||
@ -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<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) {
|
||||
|
@ -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:
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user