unable to store phone login credentials in mongodb from firebase

181 Views Asked by At

I am using firebase and mongodb atlas. I have saved social media login credentials like email, name, picture in mongodb users collection. but I am unable to save phone login credentials like phone number and uid. when I login into app using social login, I am getting id like the below image. enter image description here

but when I login into app using phone number, I am unable to get id. May be the reason the phone credentials was not saved. How can I get id value at the time of phone login. In Mongodb realm app, we store value like this. enter image description here

could you please help me with the solution. thanks in advance.

1

There are 1 best solutions below

2
On

You should use the UID that firebase provides in both cases. All forms of firebase authentication provide this property as this is the way Firebase identifies your users. If you are trying to save the firebase Auth credentials to mongodb, use the UID.

You can find the uid of a social auth provider like this.

firebase.auth()
  .signInWithPopup(provider)
  .then((result) => {
    /** @type {firebase.auth.OAuthCredential} */
    var credential = result.credential;

    // This gives you a Google Access Token. You can use it to access the Google API.
    var token = credential.accessToken;
    // The signed-in user info.
    var user = result.user;
    const uid = user.uid
    // ...
  })