signUp method

Future<UserModel?> signUp({
  1. required String email,
  2. required String password,
  3. required String fullName,
  4. required UserType userType,
  5. String? phoneNumber,
  6. String? address,
  7. String? farmName,
  8. String? farmLocation,
})

Registers a new user with Firebase Authentication and Firestore.

Creates a user account, stores profile data in Firestore, and saves the FCM token.

Parameters:

  • email: The user's email address.
  • password: The user's password.
  • fullName: The user's full name.
  • userType: The type of user (farmer or consumer).
  • phoneNumber: The user's phone number (optional).
  • address: The user's address (optional).
  • farmName: The name of the user's farm (optional).
  • farmLocation: The location of the user's farm (optional).

Returns: A Future<UserModel?> containing the created user model, or null if the operation fails.

Implementation

Future<UserModel?> signUp({
  required String email,
  required String password,
  required String fullName,
  required UserType userType,
  String? phoneNumber,
  String? address,
  String? farmName,
  String? farmLocation,
}) async {
  try {
    UserCredential result = await _auth.createUserWithEmailAndPassword(
      email: email,
      password: password,
    );

    UserModel userModel = UserModel(
      uid: result.user!.uid,
      email: email,
      fullName: fullName,
      userType: userType,
      phoneNumber: phoneNumber,
      address: address,
      farmName: farmName,
      farmLocation: farmLocation,
    );

    // Save user data to Firestore
    await _firestore
        .collection('users')
        .doc(result.user!.uid)
        .set(userModel.toMap());

    await saveFcmToken(result.user!.uid);
    return userModel;
  } catch (e) {
    print(e.toString());
    return null;
  }
}