Skip to content

Commit

Permalink
Merge pull request #8 from 90lucasgabriel/08-auth
Browse files Browse the repository at this point in the history
08 - Auth
  • Loading branch information
90lucasgabriel authored Apr 18, 2021
2 parents 5d4aea8 + 3b160c0 commit f071a24
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 24 deletions.
42 changes: 19 additions & 23 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,28 @@ void main() {
runApp(Home());
}

class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
void firebaseInit() async {
await Firebase.initializeApp();
}

@override
void initState() {
super.initState();
firebaseInit();
}
class Home extends StatelessWidget {
final Future<FirebaseApp> _firebaseInit = Firebase.initializeApp();

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Chat Flutter',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.green,
iconTheme: IconThemeData(color: Colors.green),
),
home: ChatScreen(),
return FutureBuilder(
future: _firebaseInit,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return MaterialApp(
title: 'Chat Flutter',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.green,
iconTheme: IconThemeData(color: Colors.green),
),
home: ChatScreen(),
);
}

return CircularProgressIndicator();
},
);
}
}
56 changes: 55 additions & 1 deletion lib/ui/chat_screen.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'dart:io';

import 'package:chat/widgets/input.dart';
Expand All @@ -11,8 +13,51 @@ class ChatScreen extends StatefulWidget {
}

class _ChatScreenState extends State<ChatScreen> {
final GoogleSignIn googleSignin = GoogleSignIn();
User _user;

Future<User> _getUser() async {
try {
if (_user != null) return _user;

final GoogleSignInAccount googleUser = await GoogleSignIn().signIn();
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;

final GoogleAuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);

final UserCredential userCredential =
await FirebaseAuth.instance.signInWithCredential(credential);

final User user = userCredential.user;

return user;
} catch (error) {
return null;
}
}

void _sendMessage({String value, File file}) async {
Map<String, dynamic> data = {};
final User user = await _getUser();

if (user == null) {
Widget snackbar = SnackBar(
content: Text('Não foi possível realizar o login. Tente novamente.'),
backgroundColor: Colors.red,
);

ScaffoldMessenger.of(context).removeCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(snackbar);
}

Map<String, dynamic> data = {
'senderUid': user.uid,
'senderName': user.displayName,
'senderPhotoUrl': user.photoURL,
};

if (file != null) {
UploadTask task = FirebaseStorage.instance
Expand All @@ -32,6 +77,15 @@ class _ChatScreenState extends State<ChatScreen> {
FirebaseFirestore.instance.collection('messages').add(data);
}

@override
void initState() {
super.initState();

FirebaseAuth.instance.authStateChanges().listen((user) {
_user = user;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down

0 comments on commit f071a24

Please sign in to comment.