Login/Register improvements

master
Rafael 2024-05-18 00:24:10 +02:00
parent 1cb7970982
commit 323b2c7c4c
4 changed files with 85 additions and 37 deletions

View File

@ -1,4 +1,5 @@
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
/// ///
/// Compare two lists by their content ignoring their elements order. /// Compare two lists by their content ignoring their elements order.
@ -27,3 +28,24 @@ String convertDecimalToDMS(double decimalValue) {
// return formatted string // return formatted string
return '${degrees.abs()}° ${minutes.abs()}\' ${seconds.abs()}" $direction'; 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'),
),
],
),
);
}

View File

@ -1,7 +1,9 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:cofounderella/services/auth/auth_service.dart'; import '../components/my_button.dart';
import 'package:cofounderella/components/my_button.dart'; import '../components/my_textfield.dart';
import 'package:cofounderella/components/my_textfield.dart'; import '../helper.dart';
import '../services/auth/auth_service.dart';
class LoginPage extends StatelessWidget { class LoginPage extends StatelessWidget {
//text controllers //text controllers
@ -15,24 +17,35 @@ class LoginPage extends StatelessWidget {
// login method // login method
void login(BuildContext context) async { 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 // auth service
final authService = AuthService(); final auth = AuthService();
// try login // try login
try { try {
await authService.signInWithEmailPassword( await auth.signInWithEmailPassword(
_emailController.text, _emailController.text,
_passwordController.text, _passwordController.text,
); );
} catch (e) { } on FirebaseAuthException catch (e) {
showDialog( if (context.mounted) {
context: context, showMsg(
builder: (context) => AlertDialog( context,
title: Text(e.toString()), e.code.isNotEmpty ? e.code : 'Login error',
), '${e.message}',
); );
} }
} }
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {

View File

@ -1,8 +1,9 @@
import 'package:cofounderella/services/auth/auth_service.dart'; import 'package:cofounderella/helper.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import '../components/my_button.dart'; import '../components/my_button.dart';
import '../components/my_textfield.dart'; import '../components/my_textfield.dart';
import '../services/auth/auth_service.dart';
class RegisterPage extends StatelessWidget { class RegisterPage extends StatelessWidget {
//text controllers //text controllers
@ -18,32 +19,28 @@ class RegisterPage extends StatelessWidget {
RegisterPage({super.key, required this.onTap}); RegisterPage({super.key, required this.onTap});
// register method // register method
void register(BuildContext context) { Future<void> register(BuildContext context) async {
// get auth service // get auth service
final auth = AuthService(); final auth = AuthService();
// check if passwords match // check if passwords match
if (_passwordController.text == _confirmPassController.text) { if (_passwordController.text == _confirmPassController.text) {
try { try {
auth.signUpWithEmailPassword( await auth.signUpWithEmailPassword(
_emailController.text, _emailController.text,
_passwordController.text, _passwordController.text,
); );
} catch (e) { } on FirebaseAuthException catch (e) {
showDialog( if (context.mounted) {
context: context, showMsg(
builder: (context) => AlertDialog( context,
title: Text(e.toString()), e.code.isNotEmpty ? e.code : 'Registration error',
), '${e.message}',
); );
} }
}
} else { } else {
showDialog( showMsg(context, 'Registration error', 'Passwords do not match!');
context: context,
builder: (context) => const AlertDialog(
title: Text("Passwords do not match!"),
),
);
} }
} }
@ -113,7 +110,19 @@ class RegisterPage extends StatelessWidget {
//login button //login button
MyButton( MyButton(
text: "Register", 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), const SizedBox(height: 25),

View File

@ -1,6 +1,6 @@
import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cofounderella/constants.dart';
import 'package:firebase_auth/firebase_auth.dart'; import 'package:firebase_auth/firebase_auth.dart';
import '../../constants.dart';
class AuthService { class AuthService {
// instance of auth and firestore // instance of auth and firestore
@ -12,7 +12,9 @@ class AuthService {
return _auth.currentUser; return _auth.currentUser;
} }
// sign in /// sign in (login)
///
/// @throws FirebaseAuthException
Future<UserCredential> signInWithEmailPassword(String email, password) async { Future<UserCredential> signInWithEmailPassword(String email, password) async {
try { try {
UserCredential userCredential = await _auth.signInWithEmailAndPassword( UserCredential userCredential = await _auth.signInWithEmailAndPassword(
@ -21,12 +23,14 @@ class AuthService {
); );
return userCredential; return userCredential;
} on FirebaseAuthException catch (e) { } on FirebaseAuthException {
throw Exception(e.code); rethrow;
} }
} }
// sign up (register) /// sign up (register)
///
/// @throws FirebaseAuthException
Future<UserCredential> signUpWithEmailPassword(String email, password) async { Future<UserCredential> signUpWithEmailPassword(String email, password) async {
try { try {
// create user // create user
@ -48,8 +52,8 @@ class AuthService {
); );
return userCredential; return userCredential;
} on FirebaseAuthException catch (e) { } on FirebaseAuthException {
throw Exception(e.code); rethrow;
} }
} }