Create user inside transaction

192 views
Skip to first unread message

sc...@scttnlsn.com

unread,
Feb 5, 2016, 12:31:03 PM2/5/16
to Firebase Google Group
How can I atomically create a user (via https://www.firebase.com/docs/web/api/firebase/createuser.html) and save some user profile info at path "users/<uid>" (via https://www.firebase.com/docs/web/api/firebase/set.html)?  Seems like the transaction mechanism (https://www.firebase.com/docs/web/api/firebase/transaction.html) is only useful for updating a single piece of data.  Is there a way to accomplish this?

Thanks!

-Scott

Kato Richardson

unread,
Feb 5, 2016, 12:47:54 PM2/5/16
to Firebase Google Group
Hi Scott,

See storing user profiles in the auth guide. Setting a node (i.e. saving a user profile) is a single operation, so it's pretty atomic.

Note that calling createUser() just generates a hash in the security system for creating tokens. This isn't tied in any way to your data. Since your security rules will control access, creating the hash doesn't actually grant anything, other than the ability to create a token for that email/password combination. There's no need for this to be tied via any transaction to the profile creation.

☼, Kato



--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/aae090f7-f6c7-48c5-b04d-382cf5500fd1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

sc...@scttnlsn.com

unread,
Feb 8, 2016, 11:53:21 AM2/8/16
to Firebase Google Group
Suppose the call to createUser() succeeds but the subsequent save of the profile fails (perhaps due to a loss of network connectivity).  The client application would need to explicitly handle and check for this case in order to fix the data inconsistency issue that arrises.  A transaction around both the createUser() and profile saving would make this a non-issue.

Frank van Puffelen

unread,
Feb 8, 2016, 7:56:13 PM2/8/16
to Firebase Google Group
This would be a transaction against two data sources: your database and the place where Firebase keeps the user profile. There is no such transaction mechanism in the Firebase API.

Defensive coding is typically your best bet here: don't assume that a user profile exists, create/update the profile when the user authenticates, etc.

    puf

Alex Memering

unread,
Feb 8, 2016, 8:20:03 PM2/8/16
to fireba...@googlegroups.com
It is fairly common to write the auth data every time the user authenticates because this will pick up any changes that have been made since the last login (although this method is not as helpful with email/password since you're already in control of when they can change their data).  Especially for OAuth providers since the user can update their own information through the provider at any time.

Reply all
Reply to author
Forward
0 new messages