create user without logging out and add user to organisation collection

Custom Code

Hello Community,

In my app, I have added the ability for the logged in user to create a new user without logging out, as flutter flow doesn't support it.

I have this code working, but I am not sure how to also add the newly created user to the 'organisation' document.

// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_auth/firebase_auth.dart';

Future<DocumentReference?> createUserWithoutLogin(
  String email,
  String password,
  String displayName,
) async {
  //String returnUid = "";
  DocumentReference? documentReference;

  FirebaseApp app = await Firebase.initializeApp(
      name: 'Secondary', options: Firebase.app().options);

  try {
    UserCredential userCredential = await FirebaseAuth.instanceFor(app: app)
        .createUserWithEmailAndPassword(email: email, password: password);

    User user = userCredential.user!;

    //Now create the users document because we can't do that from within FlutterFlow (annoying)
    FirebaseFirestore firestore = FirebaseFirestore.instance;
    CollectionReference users = firestore.collection('users');
    documentReference = users.doc(user.uid);

    // Data to be added to the document
    Map<String, dynamic> data = {
      'name': '${displayName}',
      'email': '${user.email}',
      'created_time': FieldValue.serverTimestamp(), // Set current server time
    };

    // Create the document
    await documentReference.set(data);

    print('Document created successfully');

    //returnUid = user.uid;
  } on FirebaseAuthException catch (e) {
    print('Error: ${e.message.toString()}');
    // Do something with exception. This try/catch is here to make sure
    // that even if the user creation fails, app.delete() runs, if is not,
    // next time Firebase.initializeApp() will fail as the previous one was
    // not deleted.
  }

  await app.delete();
  return documentReference;

  // Add your function code here!
}

This is what the document looks like:

So effectively during the above action, I want also add this user into the list document reference. But I am not sure how to subsequently do this.

Any help here would be tight!

What have you tried so far?

I tried using the code pilot, but to I also do not have enough knowledge to really take this any further since I was able to gather this from the community.

I am guessing this might not be possible, as I am returning the document reference for users, and can only seem to reference a single document type in the action settings. So perhaps this is a step too far.

Did you check FlutterFlow's Documentation for this topic?
No
2
2 replies