From 90623836c6e62ec04256dd7759f4e7c9c5bfb7e0 Mon Sep 17 00:00:00 2001 From: Rodrigo Reis Date: Fri, 20 Nov 2020 11:53:27 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Google=20auth=20done?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- whowhat/lib/pages/login.dart | 81 +++++++++++--------------- whowhat/lib/pages/signup.dart | 77 ++++++++++++------------ whowhat/lib/widgets/database/auth.dart | 40 ++++++++++++- 3 files changed, 113 insertions(+), 85 deletions(-) diff --git a/whowhat/lib/pages/login.dart b/whowhat/lib/pages/login.dart index 94e2a61..50fce94 100644 --- a/whowhat/lib/pages/login.dart +++ b/whowhat/lib/pages/login.dart @@ -18,6 +18,22 @@ class _MyLoginState extends State { final emailController = TextEditingController(); final passwordController = TextEditingController(); + checkSignIn() async { + dynamic result = await _auth.signInWithEmail( + emailController.text, passwordController.text); + if (result == null) { + print("Error signing in"); + } else { + print('signed in'); + print(result); + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => MyMenu(), + )); + } + } + @override Widget build(BuildContext context) { return Scaffold( @@ -62,60 +78,31 @@ class _MyLoginState extends State { fontSize: 18), )), Padding( - padding: EdgeInsets.all(10), - child: Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Padding( - padding: EdgeInsets.all(5), - child: InkWell( - child: Container( - height: 62, - width: MediaQuery.of(context).size.width * 0.25, - decoration: BoxDecoration( - borderRadius: BorderRadius.circular(15), - color: Color(0xFFECECEC), - ), - child: Icon(AppIcons.google, - color: Color(0xFF9B9B9B)), - ), - ), - ), - Padding( - padding: EdgeInsets.all(5), - child: InkWell( - child: Container( - height: 62, - width: MediaQuery.of(context).size.width * 0.25, - decoration: BoxDecoration( - borderRadius: BorderRadius.circular(15), - color: Color(0xFFECECEC), - ), - child: Icon(AppIcons.facebook, - color: Color(0xFF9B9B9B)), - ), + padding: EdgeInsets.all(10), + child: Padding( + padding: EdgeInsets.all(5), + child: InkWell( + child: Container( + height: 62, + width: MediaQuery.of(context).size.width * 0.25, + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(15), + color: Color(0xFFECECEC), ), + child: + Icon(AppIcons.google, color: Color(0xFF9B9B9B)), ), - ], - )), + onTap: () async { + _auth.signInWithGoogle(); + }), + ), + ), Padding( padding: EdgeInsets.all(10), child: GradientButton( text: 'Sign In', onPressed: () async { - dynamic result = await _auth.signInWithEmail( - emailController.text, passwordController.text); - if (result == null) { - print("Error signing in"); - } else { - print('signed in'); - print(result); - Navigator.push( - context, - MaterialPageRoute( - builder: (context) => MyMenu(), - )); - } + checkSignIn(); }, ), ), diff --git a/whowhat/lib/pages/signup.dart b/whowhat/lib/pages/signup.dart index f80109b..751ff07 100644 --- a/whowhat/lib/pages/signup.dart +++ b/whowhat/lib/pages/signup.dart @@ -18,6 +18,8 @@ class _MySignupState extends State { final emailController = TextEditingController(); final passwordController = TextEditingController(); + final nameController = TextEditingController(); + final jobController = TextEditingController(); @override Widget build(BuildContext context) { return Scaffold( @@ -31,7 +33,7 @@ class _MySignupState extends State { widthFactor: 0.8, child: Column( children: [ - Image(image: AssetImage('assets/images/login.png')), + /*Image(image: AssetImage('assets/images/login.png')),*/ Padding( padding: EdgeInsets.all(10), child: TextBox( @@ -51,6 +53,24 @@ class _MySignupState extends State { textInputType: TextInputType.visiblePassword, ), ), + Padding( + padding: EdgeInsets.all(10), + child: TextBox( + icon: Icons.person, + placeholder: 'Name', + controller: nameController, + textInputType: TextInputType.name, + ), + ), + Padding( + padding: EdgeInsets.all(10), + child: TextBox( + icon: Icons.cases, + placeholder: 'Job', + controller: jobController, + textInputType: TextInputType.name, + ), + ), Padding( padding: EdgeInsets.only(top: 10), child: Text( @@ -62,49 +82,32 @@ class _MySignupState extends State { fontSize: 18), )), Padding( - padding: EdgeInsets.all(10), - child: Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Padding( - padding: EdgeInsets.symmetric(vertical: 20), - child: InkWell( - child: Container( - height: 62, - width: MediaQuery.of(context).size.width * 0.25, - decoration: BoxDecoration( - borderRadius: BorderRadius.circular(15), - color: Color(0xFFECECEC), - ), - child: Icon(AppIcons.google, - color: Color(0xFF9B9B9B)), - ), - ), - ), - Padding( - padding: EdgeInsets.all(5), - child: InkWell( - child: Container( - height: 62, - width: MediaQuery.of(context).size.width * 0.25, - decoration: BoxDecoration( - borderRadius: BorderRadius.circular(15), - color: Color(0xFFECECEC), - ), - child: Icon(AppIcons.facebook, - color: Color(0xFF9B9B9B)), - ), - ), + padding: EdgeInsets.symmetric(vertical: 20), + child: InkWell( + child: Container( + height: 62, + width: MediaQuery.of(context).size.width * 0.25, + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(15), + color: Color(0xFFECECEC), ), - ], - )), + child: + Icon(AppIcons.google, color: Color(0xFF9B9B9B)), + ), + onTap: () async { + _auth.signInWithGoogle(); + }), + ), Padding( padding: EdgeInsets.all(10), child: GradientButton( text: 'Register', onPressed: () async { dynamic result = await _auth.registerWithEmail( - emailController.text, passwordController.text); + emailController.text, + passwordController.text, + nameController.text, + jobController.text); if (result == null) { print("Error signing in"); } else { diff --git a/whowhat/lib/widgets/database/auth.dart b/whowhat/lib/widgets/database/auth.dart index 02a80cb..347bbe1 100644 --- a/whowhat/lib/widgets/database/auth.dart +++ b/whowhat/lib/widgets/database/auth.dart @@ -1,7 +1,11 @@ import 'package:firebase_auth/firebase_auth.dart'; +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:google_sign_in/google_sign_in.dart'; class AuthService { final FirebaseAuth _auth = FirebaseAuth.instance; + final CollectionReference databaseReference = + FirebaseFirestore.instance.collection('users'); //Sign in Anon @@ -28,15 +32,49 @@ class AuthService { } } - Future registerWithEmail(String email, String password) async { + Future registerWithEmail( + String email, String password, String name, String job) async { try { UserCredential result = await _auth.createUserWithEmailAndPassword( email: email, password: password); User user = result.user; + + String uid = user.uid.toString(); + + await databaseReference.doc(uid).set({'name': name, 'job': job}); + return (user); } catch (e) { print(e.toString()); return null; } } + + Future getUserName() async { + if (_auth.currentUser != null) { + DocumentSnapshot ds = + await databaseReference.doc(_auth.currentUser.uid).get(); + return ds.data()['name']; + } + + return "nada"; + } + + Future signInWithGoogle() async { + // Trigger the authentication flow + final GoogleSignInAccount googleUser = await GoogleSignIn().signIn(); + + // Obtain the auth details from the request + final GoogleSignInAuthentication googleAuth = + await googleUser.authentication; + + // Create a new credential + final GoogleAuthCredential credential = GoogleAuthProvider.credential( + accessToken: googleAuth.accessToken, + idToken: googleAuth.idToken, + ); + + // Once signed in, return the UserCredential + return await FirebaseAuth.instance.signInWithCredential(credential); + } }