skip to Main Content

I’m new to asking questions in Stack overflow.

I’m implementing an application that it’s using Firebase Auth for users login, logout and delete account and mongodb with python (pymongo) to manage the database.

When a user signs in, a new user is created in Firebase Auth so the new user is given a new UID which is a 28 characters id (it is a string). My idea is to set the value of the _id from mongodb collection to the uid but it seems that the uid is longer than what it is expected:
Here you can see the output in the python console (An exception occurred :: 'g2CgrpStaOUwEFBpLzuJWme3OMR2' is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string)

Do you have any idea to solve this problem? I tried
"_id": ObjectId(str.encode(id)) when creating the document but it says
An exception occurred :: id must be an instance of (bytes, str, ObjectId), not <class 'bytes'> and I’m running out of ideas.

I hope you can help me, I’m glad to hear your questions.
Thanks in advance.

EDIT

As I’m working with Firebase Auth in the frontend (react native + expo app)
@Dharmaraj suggested me to use Firebase Auth admin SDK in the client side so that I can assign a certain uid to the user based on the user _id in mongodb database. Thanks for your suggestion!

2

Answers


  1. The MongoDB object ID is a 12 bytes hex string but Firebase auth’s UID is not. You can either store the Firebase UID as string in a different field in document e.g. userId or since your are using MongoDB I assume you must be running this logic on server side. In that case you can first add the user document in MongoDB and use that _ID as Firebase UID. You can specify this custom UID when using Firebase admin SDK

    Login or Signup to reply.
  2. The _id field is mandatory, unique within a collection, and immutable. However it does not have to be an ObjectID.

    It often is, as MongoDB will create the field automatically as one, but noting the caveats above, you can set it to a string or indeed any other valid BSON type.

    More info: https://www.mongodb.com/docs/manual/core/document/#the-_id-field

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search