From 63fe9170e34c4f163567ddae078b8e6c1ca44ad9 Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Mon, 27 Mar 2023 21:20:57 -0600 Subject: Implemented Firebase auth --- lib/database/helper.dart | 2 +- lib/firebase/email_auth.dart | 26 +++++++++++++++++++++++--- lib/screens/login_screen.dart | 31 +++++++++++++++++++------------ lib/screens/register_screen.dart | 33 ++++++++++++++++++++------------- 4 files changed, 63 insertions(+), 29 deletions(-) diff --git a/lib/database/helper.dart b/lib/database/helper.dart index 328ccf0..5992e7f 100644 --- a/lib/database/helper.dart +++ b/lib/database/helper.dart @@ -43,7 +43,7 @@ class DatabaseHelper { description VARCHAR(500), date DATE NOT NULL, completed INT DEFAULT 0 - """); +);"""); } Future insert(String table, Map data) async { diff --git a/lib/firebase/email_auth.dart b/lib/firebase/email_auth.dart index 70389ca..3b6844c 100644 --- a/lib/firebase/email_auth.dart +++ b/lib/firebase/email_auth.dart @@ -3,15 +3,35 @@ import 'package:firebase_auth/firebase_auth.dart'; class EmailAuth { final FirebaseAuth _auth = FirebaseAuth.instance; - Future createUserWithEmailAndPassword({ + Future createUserWithEmailAndPassword({ required String email, required String password, }) async { try { - UserCredential cred = await _auth.createUserWithEmailAndPassword( + await _auth.createUserWithEmailAndPassword( email: email, password: password, ); - } catch (e) {} + return true; + } catch (e) { + print(e); + return false; + } + } + + Future signInWithEmailAndPassword({ + required String email, + required String password, + }) async { + try { + UserCredential cred = await _auth.signInWithEmailAndPassword( + email: email, + password: password, + ); + return cred.user?.emailVerified == true; + } catch (e) { + print(e); + return false; + } } } diff --git a/lib/screens/login_screen.dart b/lib/screens/login_screen.dart index 986b9e3..e70e84d 100644 --- a/lib/screens/login_screen.dart +++ b/lib/screens/login_screen.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:pmsna1/firebase/email_auth.dart'; import 'package:pmsna1/widgets/loading_modal_widget.dart'; import 'package:pmsna1/widgets/responsive.dart'; import 'package:social_login_buttons/social_login_buttons.dart'; @@ -14,6 +15,8 @@ class _LoginScreenState extends State with SingleTickerProviderStateMixin { late AnimationController _controller; + final EmailAuth emailAuth = EmailAuth(); + bool isLoading = false; final padding = 16.0; @@ -86,18 +89,7 @@ class _LoginScreenState extends State buttonType: SocialLoginButtonType.generalLogin, text: 'Iniciar sesión', backgroundColor: Theme.of(context).colorScheme.primary, - onPressed: () { - setState(() { - isLoading = true; - }); - Future.delayed(const Duration(seconds: 4)) - .whenComplete(() { - setState(() { - isLoading = false; - Navigator.of(context).pushNamed('/dash'); - }); - }); - }, + onPressed: () => onLoginClicked(context), ), spacer, const Divider(), @@ -133,6 +125,21 @@ class _LoginScreenState extends State ], ); + void onLoginClicked(BuildContext context) { + emailAuth + .createUserWithEmailAndPassword( + email: _emailController.text, + password: _passwordController.text, + ) + .then((success) { + setState(() { + isLoading = false; + }); + // TODO: checar si el resultado es true + Navigator.of(context).pushNamed('/dash'); + }); + } + @override Widget build(BuildContext context) { return Scaffold( diff --git a/lib/screens/register_screen.dart b/lib/screens/register_screen.dart index 43a9de2..aa5c213 100644 --- a/lib/screens/register_screen.dart +++ b/lib/screens/register_screen.dart @@ -1,6 +1,7 @@ import 'package:email_validator/email_validator.dart'; import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; +import 'package:pmsna1/firebase/email_auth.dart'; import 'package:pmsna1/widgets/avatar_picker.dart'; import 'package:social_login_buttons/social_login_buttons.dart'; @@ -134,19 +135,7 @@ class _RegisterScreenState extends State { text: 'Crear cuenta', backgroundColor: Theme.of(context).colorScheme.primary, - onPressed: () { - setState(() { - isLoading = false; - if (validateForm()) { - Future.delayed( - const Duration(seconds: 4)) - .whenComplete(() { - Navigator.of(context) - .pushNamed('/onboard'); - }); - } - }); - }, + onPressed: () => onRegisterClicked(context), ), ], ), @@ -164,4 +153,22 @@ class _RegisterScreenState extends State { ), ); } + + void onRegisterClicked(BuildContext context) { + setState(() { + isLoading = false; + if (validateForm()) { + EmailAuth() + .createUserWithEmailAndPassword( + email: _emailController.text, + password: _passwordController.text, + ) + .then((success) { + if (success) { + Navigator.of(context).pushNamed('/dash'); + } + }); + } + }); + } } -- cgit v1.2.3