Create new users from within the app as an administrator, while not logging yourself out.

Unfortunately, when you create a new user you get automatically logged in.

If you want to act as an administrator and create logins for your users within the Flutter flow app. You need to do the following:

  • Create a cloud function & a custom action

Make a cloud function like so:

Cloud Function parameters has the following:

  • Return value: True, type JSON, not nullable

  • require authentication: True

const functions = require('firebase-functions');
const admin = require('firebase-admin');

exports.createUser = functions.https.onCall(async (data, context) => {
  // Authentication / user information is automatically added to the request.
  if (!context.auth || !context.auth.uid) {
    throw new functions.https.HttpsError('unauthenticated', 'You must be logged in to create a user.');
  }

  const { email, password, displayName } = data;

  try {
    // Create a new user
    const userRecord = await admin.auth().createUser({ email, password });
    
    // Add metadata to Firestore
    const docRef = await admin.firestore().collection('users').doc(userRecord.uid);
    
    await docRef.set({
      name: displayName,
      email: email,
      created_time: admin.firestore.FieldValue.serverTimestamp(),
    });

    return {
      status: 'SUCCESS',
      userId: userRecord.uid,
      docRef: docRef.path
    };
  } catch (error) {
    console.error('Error creating user:', error);
    throw new functions.https.HttpsError('internal', 'Internal error occurred while creating user.');
  }
});

Custom action has the following:

  • check the box 'exclude from complication'

  • make the three arguments type strings, non nullable, email password and displayName

  • No return value.

// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/backend/schema/structs/index.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!

// Set your action name, define your arguments and return parameter,
// and then add the boilerplate code using the button on the right!

import 'package:cloud_functions/cloud_functions.dart';

Future createUsers(String email, String password, String displayName) async {
  final HttpsCallable callable = FirebaseFunctions.instance.httpsCallable(
    'createUser',
  );

  try {
    final HttpsCallableResult result = await callable.call({
      'email': email,
      'password': password,
      'displayName': displayName,
    });
    print('User created: ${result.data}');
  } catch (e) {
    print('Error: $e');
  }
}

This works much better as you're invoking the Firebase SDK on the server side, thus not logging out the user on the client side.

This also adds data to the user collection doc as needed. Cheers!

13
12 replies