5
votes

I have just implemented the Twitter login to my Firebase web app. When the user successfully login, I'd like to see the user's Twitter ID (to enable Twitter-based communication among users).

According to the document (https://firebase.google.com/docs/auth/web/manage-users), the provider specific information is under providerData of the user info.

Under the providerData, I see the following

displayName: "Satoshi Nakajima" (as I expected)
email: null (as I expected)
phoneNumber: null (as I expected)
photoURL: "https://abs.twimg.com/..." (as I expected) 
providerId: "twitter.com" (of course)
uid: "1129128..." (what is this?)

The uid seems like a unique id, but it is different from the Twitter ID we usually use, such as @snakajime (which is mine).

I am wondering why I don't see the Twitter id here. Am I missing something? Is there any API to get the Twitter ID from this strange uid?

2

2 Answers

8
votes

You can actually get it immediately after sign-in, via AdditionalUserInfo.

Here is an example with the web API:

firebase.auth().signInWithPopup(new firebase.auth.TwitterAuthProvider())
  .then((userCredential) => {
    // Get the Twitter screen name.
    console.log(userCredential.additionalUserInfo.username);
  })
  .catch((error) => {
    // An error occurred.
  });
2
votes

As far as I know, the screen name you are looking for is not included as part of the Twitter OAuth provided through Firebase, but the uid that you have found is indeed the user's twitter user ID.

Using this ID you can use the Twitter API to get details about the user by passing it to the twitter API users/show endpoint. Since you plan on providing Twitter based communication, you will likely need to use this API anyway.

e.g.:

GET https://api.twitter.com/1.1/users/show.json?user_id={the_uid_from_provider_data}


RESPONSE:
{
  "id": {the_uid_from_provider_data},
  "id_str": "{the_uid_from_provider_data}",
  "name": "Snakajime",
  "screen_name": "snakajime", // <--- Here is the screen name you are looking for
  ...
}