Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using firebase auth data and adding extra info to mongodb from firebase user

I have a react app with a login page using firebase auth, where i can login using either facebook or google. This works fine, i can display the username and its avatar.

I am using mongodb, for saving other items, and i have a user model where i wish to save some extra data such as country, age etc. Can i in some way combine firebase auth user data with mongodb, so when i create a new user can i then create a new user with mongodb, but later on add extra info such as country, age etc? I guess mongodb would need to match some id with the user id?

What i have tried:

I havent tried the solution above because im unsure if it will work, or if i should just scrap firebase auth, and just use mongodb for my authentication.

like image 306
Alexander Munck Af Rosenschöld Avatar asked Apr 14 '19 19:04

Alexander Munck Af Rosenschöld


People also ask

Can I use Firebase Auth with MongoDB?

While MongoDB Realm offers a built-in authentication mechanism, I personally prefer to use Google's Firebase authentication method. It is pretty simple to integrate Firebase with MongoDB Realm for authentication if you know how.

How do I add more fields in Firebase authentication?

If you want to add additional data, you need to create a model class for your user and then store it in your Firebase database. What about using the setDisplayName method to store custom fields as a JSON Object like this example {"display_name": "User 1", "age": 25, "gender": "Male", "points": 5371} ?

Why are you using Firebase `? What other options do you have to implement authentication?

Firebase Authentication provides backend services, easy-to-use SDKs, and ready-made UI libraries to authenticate users to your app. It supports authentication using passwords, phone numbers, popular federated identity providers like Google, Facebook and Twitter, and more.

Does Firebase Auth store user data?

User properties Firebase users have a fixed set of basic properties—a unique ID, a primary email address, a name and a photo URL—stored in the project's user database, that can be updated by the user (iOS, Android, web).


1 Answers

It is fairly common to store user information in a separate database. Developers do this both for extra properties and to allow querying user profiles from within your app, which the client-side libraries for Firebase Authentication don't allow.

The most common databases for such information are the ones that are part of Firebase itself (Realtime Database, and Cloud Firestore), but it can be stored in MongoDB just as well. The only requirement is that you associate the user profile information in the database with the user profile in Firebase Authentication, typically by using the Firebase Authentication UID of the user as the key in the database.

For some examples of this (typically for Firebase Realtime Database, but the approach is the same for any database), see:

  • How do I return a list of users if I use the Firebase simple username & password authentication
  • Firebase: setting additional user properties
  • Firebase - Adding additional user data to separate DB
  • Add Extra Details on Firebase User Table
like image 159
Frank van Puffelen Avatar answered Sep 23 '22 04:09

Frank van Puffelen