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: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'),
),
],
),
);
}

View File

@ -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,24 +17,35 @@ 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}',
);
}
}
}
@override
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: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<void> 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),

View File

@ -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<UserCredential> 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<UserCredential> 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;
}
}