1
votes

How do I map a Facebook friend id to a Firebase uid in the realtime database? It's my understanding that the Firebase uid is not the same as a Facebook id.

My current user flow is logging into Facebook through the Facebook sdk, and then passing the facebook access token to the Firebase sdk to login with Firebase.

My end goal is to store game scores so that a user can see the scores of their friends as well as their own. I do not want to request every score for every player and filter for this information on the client. I would rather send X queries for X amount of friends and only request the scores desired.

1
Do you want to make the FacebookId a property of the Firebase User? The way I would manage this is to have a property in the database object called "FacebookID".Quintin Balsdon
I thought about storing the FacebookId in the database like so. That would let me do a query for each $uid with the Facebook ids I already have, then use that $uid to lookup the scores. Does anyone have a better solution? fbusers { $fbid { $uid } }WakeZero

1 Answers

6
votes

Not sure what the downvote was for, but here is the solution I ended up using.

Storing the scores by facebookId instead of firebaseId allows the lookup of scores of friends, since a facebookId can't be mapped to a firebaseId without storing it in the database anyway. It looks like this:

facebookUsers {
  facebookId {
    "firebaseId" : firebaseId,
    "scores" : {
      level : score,
    }
  }
}

This structure allows you to easily lookup scores by facebookId, and also map facebookIds to firebaseIds if desired for other operations.

To save scores:

FirebaseDatabase.DefaultInstance
  .GetReference("facebookUsers")
  .Child(facebookId)
  .Child("scores")
  .Child(level)
  .SetValueAsync(score);

To lookup scores:

FirebaseDatabase.DefaultInstance
  .GetReference("facebookUsers")
  .Child(facebookId)
  .Child("scores")
  .Child(level)
  .GetValueAsync();

To restrict writes based on facebookId, use the following rules:

{
  "rules": {
    ".read": "auth != null",
    "facebookUsers": {      
      "$facebookId" : {
        ".write": "data.child('firebaseId').val() == auth.uid || (!data.exists() && newData.child('firebaseId').val() == auth.uid)",
      }
    }
  }
}

Hopefully this helps someone more than a downvote.