How should I structure my User Model with Firebase Auth and MongoDB?

I am using firebase for auth only and custom backend with express.js and mongodb.

Firebase Auth stores the name, email, password, photo, phone when ever a new user SignUp. no matter its Google OAuth or Old style email Signup.

But still as I also have to keep the record of user of my server so Firebase returns an UID to my server so that I can save it in my DB.

I wanna know as I am using MongoDB for my User Model should use the default _id generated by MongoDB on every new document? Or should I overwrite that default MongoDB _id with firebase uid ? Or should I store both the Ids ?

Also should I also save the email, name, phone, photo of user in my User Model or just leave it to firebase only?

So I’ve worked on projects in the past with a similar setup, as firebase auth makes it rather easy to deal with authentication without having to store passwords (yay) among other things.

You should store the user information within the database, this way if your app shows a list of users’s, you have all their information last saved from google/login. If you actually need this sort of data beyond the current user is more up to what you app does, but if you need it anywhere beyond the current user, you’d probably want to re-save those properties.

The answer to this is more up in the air, you could save both, or pick one. On the project we built we put the mongoId in firebase, as we wanted better performance in mongodb by using their _id rather than firebase. The trade offs are super minor, so you could go either route, or even save both on the record (it just might get confusing).

So yea, there isn’t really a solid answer AFAIK for which id you store, and where and what. As long as you keep track of who is who between firebase and your database reliably it doesn’t matter that much.

1 Like

Thanks your experience was really helpful. I will go with storing both the IDs and User’s last info. Might not need to retrieve that info as currentUser() on the client will fill these needs.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.