From a22de28828eedd468eb483433fc1771713babf06 Mon Sep 17 00:00:00 2001 From: lenovo Date: Mon, 14 Oct 2024 00:14:08 +0530 Subject: [PATCH] Fixed #59 --- lib/cubit/auth/auth_cubit.dart | 49 ++++++++++++++++++++++++---- lib/views/pages/login/login.dart | 17 ++++++---- lib/views/pages/register/signup.dart | 12 ++++--- 3 files changed, 59 insertions(+), 19 deletions(-) diff --git a/lib/cubit/auth/auth_cubit.dart b/lib/cubit/auth/auth_cubit.dart index 4fbbdb5..70348d3 100644 --- a/lib/cubit/auth/auth_cubit.dart +++ b/lib/cubit/auth/auth_cubit.dart @@ -1,3 +1,4 @@ +import 'package:donorconnect/secrets.dart'; import 'package:donorconnect/views/pages/welcome/welcome_screen.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; @@ -7,6 +8,8 @@ import 'package:firebase_auth/firebase_auth.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:shared_preferences/shared_preferences.dart'; +import '../../views/pages/register/signup.dart'; + class AuthCubit extends Cubit { final FirebaseAuth _auth; final FirebaseFirestore _firestore; @@ -20,6 +23,25 @@ class AuthCubit extends Cubit { } }); } + Route _createRoute(Widget page) { + return PageRouteBuilder( + pageBuilder: (context, animation, secondaryAnimation) => page, + transitionsBuilder: (context, animation, secondaryAnimation, child) { + const begin = 0.0; + const end = 6.0; + const curve = Curves.easeInOut; + + var tween = + Tween(begin: begin, end: end).chain(CurveTween(curve: curve)); + return FadeTransition( + opacity: animation.drive(tween), + child: child, + ); + }, + transitionDuration: + const Duration(milliseconds: 700), // Increase the duration to 700ms + ); + } Future loginUser(String email, String password) async { emit(AuthLoading()); @@ -28,8 +50,14 @@ class AuthCubit extends Cubit { email: email, password: password); // After signing in, get the user data _getUserData(userCredential.user!.uid); - } catch (e) { - emit(AuthError(e.toString())); + } on FirebaseAuthException catch (e) { + if (e.code == 'user-not-found') { + emit(const AuthError("No User Found for that Email")); + } else if (e.code == 'invalid-credential') { + emit(const AuthError("Invalid mail or password Provided by User")); + } else { + emit(AuthError(e.toString())); + } } } @@ -65,11 +93,18 @@ class AuthCubit extends Cubit { // Save user data to SharedPreferences await _saveUserToPrefs(userCredential.user!.uid, userModel); - - emit(Authenticated(userModel)); // Emit authenticated state with user model - } catch (e) { - emit(AuthError(e.toString())); + emit( + Authenticated(userModel)); // Emit authenticated state with user model + } on FirebaseAuthException catch (e) { + if (e.code == 'weak-password') { + emit(const AuthError("Password Provided is too Weak")); + } else if (e.code == 'email-already-in-use') { + emit(const AuthError("User with this credential already exists")); + } else { + print(e.code); + emit(AuthError(e.toString())); + } } } @@ -112,7 +147,7 @@ class AuthCubit extends Cubit { Future _saveUserNameToPrefs(String userId, String name) async { final prefs = await SharedPreferences.getInstance(); - + print(name); await prefs.setString('${userId}_name', name); } diff --git a/lib/views/pages/login/login.dart b/lib/views/pages/login/login.dart index a6073f0..e335de9 100644 --- a/lib/views/pages/login/login.dart +++ b/lib/views/pages/login/login.dart @@ -1,6 +1,7 @@ import 'package:donorconnect/Utils/show_snackbar.dart'; import 'package:donorconnect/cubit/auth/auth_cubit.dart'; import 'package:donorconnect/cubit/auth/auth_state.dart'; +import 'package:donorconnect/secrets.dart'; import 'package:donorconnect/views/pages/main_home/homepage.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; @@ -37,10 +38,9 @@ class _LoginPageState extends State { Future loginUser() async { if (emailController.text.isNotEmpty && passwordController.text.isNotEmpty) { - context.read().loginUser( - emailController.text, - passwordController.text, - ); + context + .read() + .loginUser(emailController.text, passwordController.text); } else { showSnackBar( context, @@ -73,17 +73,20 @@ class _LoginPageState extends State { Navigator.pushReplacement( context, PageRouteBuilder( - pageBuilder: (context, animation, secondaryAnimation) => HomePage( + pageBuilder: (context, animation, secondaryAnimation) => + HomePage( email: emailController.text, name: state.user.name, ), - transitionsBuilder: (context, animation, secondaryAnimation, child) { + transitionsBuilder: + (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: animation, child: child, ); }, - transitionDuration: const Duration(milliseconds: 900), // Adjust duration as needed + transitionDuration: const Duration( + milliseconds: 900), // Adjust duration as needed ), ); } diff --git a/lib/views/pages/register/signup.dart b/lib/views/pages/register/signup.dart index bd81b90..dcda084 100644 --- a/lib/views/pages/register/signup.dart +++ b/lib/views/pages/register/signup.dart @@ -151,23 +151,25 @@ class _SignuppageState extends State { showSnackBar(context, state.message); } if (state is Authenticated) { - Navigator.pushReplacement( + Navigator.pushReplacement( context, PageRouteBuilder( - pageBuilder: (context, animation, secondaryAnimation) => HomePage( + pageBuilder: (context, animation, secondaryAnimation) => + HomePage( name: nameController.text, email: emailController.text.trim(), ), - transitionsBuilder: (context, animation, secondaryAnimation, child) { + transitionsBuilder: + (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: animation, child: child, ); }, - transitionDuration: const Duration(milliseconds: 900), // Adjust duration as needed + transitionDuration: const Duration( + milliseconds: 900), // Adjust duration as needed ), ); - } }, builder: (context, state) {