When a user signs up for your app, Firebase creates their profile in Firebase Authentication. This is great because most of the hard stuff like OAuth and validation is being handled automatically for you.
This, however, has its limitations. Auth doesn’t let you store any custom properties to the user and this is very limited in scope.
What if you want to store more information about the user whenever they perform an action on your app?
This is where the requirement of creating user profiles upon sign up comes into play.
Whenever a user signs up on your app, you can use the auto-triggering Firebase Cloud Functions to create their profile in the database.
In this tutorial, I will be using the Cloud Firestore, but the same concepts apply to the Realtime Database too if you are using that.
Firebase auth has two events for which we can create Cloud Functions.
Whenever a user signs up on your app, (be it email, OTP, or a 3rd party) Firebase triggers the
onCreate function automatically.
When a user deletes their account, Firebase triggers the
For this example, we will be focusing on the
onCreate() function because this is the trigger which you can use to create a user profile in the Cloud Firestore.
index.js, add a function as follows:
const functions = require('firebase-functions');
Of course, it is not good to have all your code in the
index.js, so this is not a perfect solution. I have written an article about splitting Cloud Functions before. Follow that.
Once this profile there in the Firestore, you can always use it to store more data about your user.
For the example, I used the
uid as the doc-id of the document inside the
Users collection. You can leave it blank which will give the document an
auto-id. Or, you can simply use something else, like the
phoneNumber or the
Make sure that you always
return a promise from the auto-triggering Cloud Functions. Not doing so will result in unstable behavior where sometimes, Firebase will or will not create the document in Firestore.