1

I am wrapping some updates inside a transaction to make them atomic. For example, when a user updates his displayName (which is stored inside a users collection), displayName of firebase auth is also to be updated. I want to do both of these updates inside a transaction. All the examples I have seen so far for transactions is about updating some document data inside some collection. How about wrapping auth functions inside a transaction or associating them with existing transactions? Is it possible?

I have this snippet in place:

        db.runTransaction((transaction)=>{
            return transaction.get(displayNameCheckRef).then((snapshot)=>{
                var newDisplayName = this.state.name;
                if(snapshot.empty){
                    transaction.set(currentUserRef, {displayName:newDisplayName});
                }
            });
        })
        .then((newDisplayName)=>{
            console.log("display name changed to " + newDisplayName);
        })
        .catch((error)=>{
            console.log("transaction error.");
            console.log(error);
        });

Now I want to associate the below updateProfile function with the transaction above. How do I do this?

   user.updateProfile({
       displayName: this.state.name
   }).then(() => {
     //do some stuff here
   });

Thanks a lot!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
honor
  • 7,378
  • 10
  • 48
  • 76

1 Answers1

0

What you're trying to do is not possible. Firestore transactions are only capable of working with documents in a single database associated with the project. They can't affect other products, such as Firebase Authentication or Cloud Storage for Firebase.

If you need to have an atomic operation cross several products, your code will need to manually roll back updates from multiple products, and those rollbacks can still have errors, so you will need to come up with some fallback behavior if something fails.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441