Mapping Facebook friend id to Firebase uid in real

2020-07-30 03:30发布

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条回答
一夜七次
2楼-- · 2020-07-30 03:40

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.

查看更多
登录 后发表回答