Search code examples
javaandroidfirebasefirebase-authenticationgoogle-cloud-firestore

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


I have a scenario where I need to create a user using the Auth and add more info (such as name and type of business) to Firestore database. Firestore as transaction/batch operation but it is for Firestore only.

Is there a way where I can perform Auth operation such as create along with the Firestore data insert. Both operation has to succeed or fail. Right now I chain them but I would be worried that one fails and the other success in which a will need to handle gracefully.

Thank you


Solution

  • 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. In other words, you will create the user only if the authentication is successful. So if isSuccessful() either all of the operations beneath one of those 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 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.

    In 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 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 a user. In 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 register a function using onDelete() method.

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