How to create user and upload data on Firebase in one single transaction

后端 未结 4 1710
情书的邮戳
情书的邮戳 2021-01-20 14:28

I am making a web application which creates a user on Firebase using email and password firebase.auth().createUserUsingEmailAndPassowrd()

After the account

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-20 14:57

    firebaser here

    There are no cross-feature transactions in Firebase at the moment. The common approach is to:

    • reduce the risk of cross-feature problems by carefully ordering your code
    • increase the resilience of your code against cross-feature failures

    This means you decide which one if more important: that each existing user has a database node or that each database node points to an existing user. You seem to want to answer "both", but as said: that's not possible, you'll have to pick. In most scenarios I've seen developers will pick option 1: every Firebase Authentication user must have a node in the database.

    In that case, you'll want to run the code that registers a user in the database before actually registering the user with Firebase Authentication. And since the user only gets their UID once they do register with Auth, you'll need some two-stepped process here. All quite nasty code that is bound to have problems. Which is why you'll see that most developers with an app in production care a lot less about this approach than you think.

    Instead they rely on regular clean-up of their database. They run an administrative process every day or so that scans the database for users without a corresponding Firebase Authentication registration. This handles all kinds of edge cases with a fairly simple process, even edge cases not related to registration. You might even call this "eventual consistency": eventually the data in the database will match the registered users.

    Final shout-out on your original question: you can drastically reduce the chance of a cross-feature failure by running the register-and-write-to-database code in a Cloud Function for Firebase. Using the Admin SDK in a Cloud Function makes this all nice and tidy. But while the chance of failures reduces a lot, it can still fail. So if you care about the cleanliness of your data, you'll still need the clean-up process.

提交回复
热议问题