signIn method
Signs in a user with Firebase Authentication and retrieves their profile.
Authenticates the user and fetches their profile data from Firestore.
Parameters:
- email: The user's email address.
- password: The user's password.
Returns: A Future<UserModel?> containing the signed-in user model, or null if the operation fails.
Throws:
- AuthException if authentication fails or an error occurs.
Implementation
Future<UserModel?> signIn({
required String email,
required String password,
}) async {
try {
UserCredential result = await _auth.signInWithEmailAndPassword(
email: email,
password: password,
);
DocumentSnapshot doc =
await _firestore.collection('users').doc(result.user!.uid).get();
await saveFcmToken(result.user!.uid);
return UserModel(
uid: result.user!.uid,
email: doc['email'],
fullName: doc['fullName'],
userType: UserTypeExtension.fromString(doc['userType']),
phoneNumber: doc['phoneNumber'],
address: doc['address'],
farmName: doc['farmName'],
farmLocation: doc['farmLocation'],
);
} on FirebaseAuthException catch (e) {
throw AuthException(e.message ?? '');
} catch (e) {
throw AuthException('An unexpected error occurred');
}
}