Skip to content

Commit

Permalink
Merge pull request #63 from ezsarthak/main
Browse files Browse the repository at this point in the history
Fixed #59
  • Loading branch information
prajapatihet authored Oct 14, 2024
2 parents a833501 + a22de28 commit 5f45266
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 19 deletions.
49 changes: 42 additions & 7 deletions lib/cubit/auth/auth_cubit.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<AuthState> {
final FirebaseAuth _auth;
final FirebaseFirestore _firestore;
Expand All @@ -20,6 +23,25 @@ class AuthCubit extends Cubit<AuthState> {
}
});
}
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<void> loginUser(String email, String password) async {
emit(AuthLoading());
Expand All @@ -28,8 +50,14 @@ class AuthCubit extends Cubit<AuthState> {
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()));
}
}
}

Expand Down Expand Up @@ -65,11 +93,18 @@ class AuthCubit extends Cubit<AuthState> {

// 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()));
}
}
}

Expand Down Expand Up @@ -112,7 +147,7 @@ class AuthCubit extends Cubit<AuthState> {

Future<void> _saveUserNameToPrefs(String userId, String name) async {
final prefs = await SharedPreferences.getInstance();

print(name);
await prefs.setString('${userId}_name', name);
}
Expand Down
17 changes: 10 additions & 7 deletions lib/views/pages/login/login.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -37,10 +38,9 @@ class _LoginPageState extends State<LoginPage> {

Future<void> loginUser() async {
if (emailController.text.isNotEmpty && passwordController.text.isNotEmpty) {
context.read<AuthCubit>().loginUser(
emailController.text,
passwordController.text,
);
context
.read<AuthCubit>()
.loginUser(emailController.text, passwordController.text);
} else {
showSnackBar(
context,
Expand Down Expand Up @@ -73,17 +73,20 @@ class _LoginPageState extends State<LoginPage> {
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
),
);
}
Expand Down
12 changes: 7 additions & 5 deletions lib/views/pages/register/signup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,25 @@ class _SignuppageState extends State<Signuppage> {
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) {
Expand Down

0 comments on commit 5f45266

Please sign in to comment.