Mapping Firebase Auth users to Firestore Documents
I am not sure how to do this in android, but with JS I am doing the following:
firebase.firestore().collection('users').doc(currentUser.uid).set(currentUser)
currentUser
is the user object that the authentication function returns (I use signInWithCredential
)
It is working perfectly. I am sure you can implement a similar approach in android.
![Nevermore](https://i.stack.imgur.com/C3vUB.png?s=256&g=1)
Nevermore
Updated on June 06, 2022Comments
-
Nevermore about 2 years
I'm trying to use Firebase's
Firestore
database to handleUser
s in my Android app.Initially I want to use the id returned from the
Auth
package (a string), and set that as the id for theusers
Collection
in the database. When creating aDocument
inside of theCollection
users
, I set auid
field to that auth package string.I realize there is no way of setting that string to an indexable value so that queries like:
// get Auth package uid string db.collection("users").document(auth_uid_string);
would work.
So the alternative, I guess, is to use
Query.whereEqualTo("uid", auth_uid_string)
which would give me the user in a list, because firestore doesn't assume the query is for a unique value.I want to avoid the above solution, and devise a way to store the
users
of the app infirestore
, and use theauth
package to verify that I fetch the correct user. Is such a solution possible? Or should I try a different firebase service, or even just run a postgres server in heroku or something? -
Nevermore over 6 yearsYeah, so i can set the index with
set
. Thanks -
tapizquent almost 5 yearsI'm not sure this is the best way to do it as Firebase explicitly says: // The user's ID, unique to the Firebase project. // Do NOT use this value to authenticate with your backend server, // if you have one. Use getTokenWithCompletion:completion: instead.
-
Edric over 4 years@JoseTapizquent That's for authenticating with your backend server, as what the comment explicitly mentions. It's fine to use the user's ID for declaring documents.
-
ckeeney over 4 yearsI'm not sure this still works. I don't think
currentUser
is serializable. I getFirebaseError: Function DocumentReference.set() called with invalid data. Data must be an object, but it was: a custom Q object