From 323b2c7c4c27b75955fe92a6c935aadd4b1960e3 Mon Sep 17 00:00:00 2001 From: Rafael <1024481@stud.hs-mannheim.de> Date: Sat, 18 May 2024 00:24:10 +0200 Subject: [PATCH] Login/Register improvements --- lib/helper.dart | 22 ++++++++++++++ lib/pages/login_page.dart | 37 ++++++++++++++++-------- lib/pages/register_page.dart | 45 +++++++++++++++++------------ lib/services/auth/auth_service.dart | 18 +++++++----- 4 files changed, 85 insertions(+), 37 deletions(-) diff --git a/lib/helper.dart b/lib/helper.dart index e3d74de..0c56871 100644 --- a/lib/helper.dart +++ b/lib/helper.dart @@ -1,4 +1,5 @@ import 'package:collection/collection.dart'; +import 'package:flutter/material.dart'; /// /// Compare two lists by their content ignoring their elements order. @@ -27,3 +28,24 @@ String convertDecimalToDMS(double decimalValue) { // return formatted string return '${degrees.abs()}° ${minutes.abs()}\' ${seconds.abs()}" $direction'; } + +/// +/// Show a simple message dialog +/// +void showMsg(BuildContext context, String title, String content) { + showDialog( + context: context, + builder: (context) => AlertDialog( + title: Text(title), + content: Text(content), + actions: [ + TextButton( + onPressed: () { + Navigator.of(context).pop(); + }, + child: const Text('OK'), + ), + ], + ), + ); +} \ No newline at end of file diff --git a/lib/pages/login_page.dart b/lib/pages/login_page.dart index d6a3115..228a9f2 100644 --- a/lib/pages/login_page.dart +++ b/lib/pages/login_page.dart @@ -1,7 +1,9 @@ +import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; -import 'package:cofounderella/services/auth/auth_service.dart'; -import 'package:cofounderella/components/my_button.dart'; -import 'package:cofounderella/components/my_textfield.dart'; +import '../components/my_button.dart'; +import '../components/my_textfield.dart'; +import '../helper.dart'; +import '../services/auth/auth_service.dart'; class LoginPage extends StatelessWidget { //text controllers @@ -15,22 +17,33 @@ class LoginPage extends StatelessWidget { // login method void login(BuildContext context) async { + if (_emailController.text.trim().isEmpty || + _passwordController.text.trim().isEmpty) { + showMsg( + context, + 'Missing information', + 'Please enter your email and password', + ); + return; + } + // auth service - final authService = AuthService(); + final auth = AuthService(); // try login try { - await authService.signInWithEmailPassword( + await auth.signInWithEmailPassword( _emailController.text, _passwordController.text, ); - } catch (e) { - showDialog( - context: context, - builder: (context) => AlertDialog( - title: Text(e.toString()), - ), - ); + } on FirebaseAuthException catch (e) { + if (context.mounted) { + showMsg( + context, + e.code.isNotEmpty ? e.code : 'Login error', + '${e.message}', + ); + } } } diff --git a/lib/pages/register_page.dart b/lib/pages/register_page.dart index 6476357..56edad2 100644 --- a/lib/pages/register_page.dart +++ b/lib/pages/register_page.dart @@ -1,8 +1,9 @@ -import 'package:cofounderella/services/auth/auth_service.dart'; +import 'package:cofounderella/helper.dart'; import 'package:flutter/material.dart'; - +import 'package:firebase_auth/firebase_auth.dart'; import '../components/my_button.dart'; import '../components/my_textfield.dart'; +import '../services/auth/auth_service.dart'; class RegisterPage extends StatelessWidget { //text controllers @@ -18,32 +19,28 @@ class RegisterPage extends StatelessWidget { RegisterPage({super.key, required this.onTap}); // register method - void register(BuildContext context) { + Future register(BuildContext context) async { // get auth service final auth = AuthService(); // check if passwords match if (_passwordController.text == _confirmPassController.text) { try { - auth.signUpWithEmailPassword( + await auth.signUpWithEmailPassword( _emailController.text, _passwordController.text, ); - } catch (e) { - showDialog( - context: context, - builder: (context) => AlertDialog( - title: Text(e.toString()), - ), - ); + } on FirebaseAuthException catch (e) { + if (context.mounted) { + showMsg( + context, + e.code.isNotEmpty ? e.code : 'Registration error', + '${e.message}', + ); + } } } else { - showDialog( - context: context, - builder: (context) => const AlertDialog( - title: Text("Passwords do not match!"), - ), - ); + showMsg(context, 'Registration error', 'Passwords do not match!'); } } @@ -113,7 +110,19 @@ class RegisterPage extends StatelessWidget { //login button MyButton( text: "Register", - onTap: () => register(context), + onTap: () => { + if (_emailController.text.trim().isEmpty || + _passwordController.text.trim().isEmpty) + { + showMsg( + context, + 'Missing information', + 'Please enter your name, email and password', + ) + } + else + {register(context)} + }, ), const SizedBox(height: 25), diff --git a/lib/services/auth/auth_service.dart b/lib/services/auth/auth_service.dart index 5ebc3ba..f701c1a 100644 --- a/lib/services/auth/auth_service.dart +++ b/lib/services/auth/auth_service.dart @@ -1,6 +1,6 @@ import 'package:cloud_firestore/cloud_firestore.dart'; -import 'package:cofounderella/constants.dart'; import 'package:firebase_auth/firebase_auth.dart'; +import '../../constants.dart'; class AuthService { // instance of auth and firestore @@ -12,7 +12,9 @@ class AuthService { return _auth.currentUser; } - // sign in + /// sign in (login) + /// + /// @throws FirebaseAuthException Future signInWithEmailPassword(String email, password) async { try { UserCredential userCredential = await _auth.signInWithEmailAndPassword( @@ -21,12 +23,14 @@ class AuthService { ); return userCredential; - } on FirebaseAuthException catch (e) { - throw Exception(e.code); + } on FirebaseAuthException { + rethrow; } } - // sign up (register) + /// sign up (register) + /// + /// @throws FirebaseAuthException Future signUpWithEmailPassword(String email, password) async { try { // create user @@ -48,8 +52,8 @@ class AuthService { ); return userCredential; - } on FirebaseAuthException catch (e) { - throw Exception(e.code); + } on FirebaseAuthException { + rethrow; } }