createUser method

Future<void> createUser(
  1. UserModel user
)

This method creates a new user record in Firestore using the current authenticated user's UID. @param user The UserModel object containing user details to be stored.

Implementation

Future<void> createUser(UserModel user) async {
  try {
    // Get the current authenticated user's UID
    final auth = FirebaseAuth.instance;
    final userId = auth.currentUser?.uid;

    if (userId == null) {
      throw Exception('User is not authenticated');
    }

    // Use the UID as the document ID in Firestore
    await _db.collection("users").doc(userId).set(user.toJson());
  } on FirebaseAuthException catch (e) {
    final result = TExceptions.fromCode(e.code);
    throw result.message;
  } on FirebaseException catch (e) {
    throw e.message.toString();
  } catch (e) {
    throw e
        .toString()
        .isEmpty ? 'Something went wrong. Please Try Again' : e.toString();
  }
}