Create user with Auth and write to Firestore at the same time

The best way to do scenarios like this is to use Firebase Cloud Functions. Cloud functions are triggered on various create/update/delete/write operations. You can trigger a cloud function on new user creation and then do the related Auth creation inside it.

https://firebase.google.com/docs/firestore/extend-with-functions

So in this case, you create user data first and in its success, call the Firebase Auth user creation. If you are worried about failure scenarios, you can remove the created user if in case the auth creation fails.


When you are implementing Firebase authentication you can use a addOnCompleteListener or addOnSuccessListener and implement either the isComplete() or isSuccessful() methods. Inside one of these methods you need to create the user in your Firestore database. With other words, you will create the user only if the authentication is successful. So if isSuccessful() either all of the operations beneath one of that methods succeed, or none of them are applied.

The difference between those 2 methods is that we can that a Task is complete when the work represented by that Task is finished, regardless of its success or failure. There may be or may be not an error but we need to check for that. A Task is successful when the work represented by the task is finished, as expected, with no errors.

As o conclusion, there is no way to have a bulk solution. This is because there are 2 separate operations. So in order to be able to write user details in the database you need to be first successfully authenticated. If you want a rollback then you need to use again an addOnCompleteListener when you write user details to the database. If the you get an error just sin-out the user. There is no transaction/batch operation that can handle authentication and a database write operation as you described.

Edit:

Since you say you are using Firebase authentication, you can also use a function, written in Cloud Function to reach your goal. You can write a function that will run every time something in your project happens. One of those "things", is the creation of an user. With other words, you can execute your logic every time a new user is created. Here's an example:

exports.updateUserCount = functions.auth.user().onCreate((user) => {
  //Do what you need to do
});

Also, you can do the same thing on a user deletion, but registering a function using onDelete() method.

This will be very fast because there is no interaction with user's device. Please see more information about Firebase Cloud Functions.