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/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" />
|
||||||
|
@ -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,20 +75,38 @@ 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) {
|
||||||
|
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(
|
body: new PageView(
|
||||||
children: [
|
children: [
|
||||||
new Container(
|
new Container(
|
||||||
color: Colors.white,
|
color: Colors.white,
|
||||||
child: new Feed(),
|
child: new Feed(),
|
||||||
),
|
),
|
||||||
new Container(color: Colors.green,),
|
new Container(
|
||||||
|
color: Colors.green,
|
||||||
|
),
|
||||||
new Container(color: Colors.white, child: new Uploader()),
|
new Container(color: Colors.white, child: new Uploader()),
|
||||||
new Container(color: Colors.amber),
|
new Container(color: Colors.amber),
|
||||||
new Container(color: Colors.white, child: new PostForm()),
|
new Container(color: Colors.white, child: new Text('Test')),
|
||||||
],
|
],
|
||||||
controller: _pageController,
|
controller: _pageController,
|
||||||
physics: new NeverScrollableScrollPhysics(),
|
physics: new NeverScrollableScrollPhysics(),
|
||||||
@ -86,6 +141,18 @@ class _HomePageState extends State<HomePage> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void login() async {
|
||||||
|
await _ensureLoggedIn();
|
||||||
|
setState(() {
|
||||||
|
triedSilentLogin = true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void silentLogin() async {
|
||||||
|
await _silentLogin();
|
||||||
|
setState(() {});
|
||||||
|
}
|
||||||
|
|
||||||
void navigationTapped(int page) {
|
void navigationTapped(int page) {
|
||||||
//Animating Page
|
//Animating Page
|
||||||
_pageController.jumpToPage(page);
|
_pageController.jumpToPage(page);
|
||||||
|
@ -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:
|
||||||
|
@ -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
|
||||||
|
Reference in New Issue
Block a user