diff options
author | Iván Ávalos <avalos@disroot.org> | 2023-03-27 21:20:57 -0600 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2023-03-27 21:20:57 -0600 |
commit | 63fe9170e34c4f163567ddae078b8e6c1ca44ad9 (patch) | |
tree | 3c6f4ffbc68e6cec96b3b075ea4300f019ee7c61 | |
parent | 67bd958602705d834a031f4dfadb9dbe6633d54e (diff) | |
download | pmsna1-63fe9170e34c4f163567ddae078b8e6c1ca44ad9.tar.gz pmsna1-63fe9170e34c4f163567ddae078b8e6c1ca44ad9.tar.bz2 pmsna1-63fe9170e34c4f163567ddae078b8e6c1ca44ad9.zip |
Implemented Firebase auth
-rw-r--r-- | lib/database/helper.dart | 2 | ||||
-rw-r--r-- | lib/firebase/email_auth.dart | 26 | ||||
-rw-r--r-- | lib/screens/login_screen.dart | 31 | ||||
-rw-r--r-- | 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<int> insert(String table, Map<String, dynamic> 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<dynamic> createUserWithEmailAndPassword({ + Future<bool> 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<bool> 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<LoginScreen> with SingleTickerProviderStateMixin { late AnimationController _controller; + final EmailAuth emailAuth = EmailAuth(); + bool isLoading = false; final padding = 16.0; @@ -86,18 +89,7 @@ class _LoginScreenState extends State<LoginScreen> 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<LoginScreen> ], ); + 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<RegisterScreen> { 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<RegisterScreen> { ), ); } + + 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'); + } + }); + } + }); + } } |