Skip to content

Instantly share code, notes, and snippets.

@xjjon
Last active December 24, 2020 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xjjon/8f3b830fb556b6f9b8b0e43439aa9e71 to your computer and use it in GitHub Desktop.
Save xjjon/8f3b830fb556b6f9b8b0e43439aa9e71 to your computer and use it in GitHub Desktop.
namespace Cloud
{
public class FacebookLogin : MonoBehaviour
{
public void Login()
{
if (FB.IsLoggedIn)
{
FB.LogOut();
}
}
private void OnFacebookLoggedIn(ILoginResult result)
{
if (result != null && string.IsNullOrEmpty(result.Error))
{
// check if linked to any existing accounts
PlayFabClientAPI.GetPlayFabIDsFromFacebookIDs(new GetPlayFabIDsFromFacebookIDsRequest
{
FacebookIDs = new List<string> {result.AccessToken.UserId},
}, dsResult =>
{
Debug.Log(dsResult.ToJson());
if (dsResult.Data.Count == 0)
{
Debug.Log("No attached facebook accounts");
LinkAccount(true, null);
}
else
{
if (_playfabIds[0].PlayFabId == PlayFabLogin.LoginResult.PlayFabId)
{
Debug.Log("Already linked to this account!");
}
else
{
Debug.Log("Linked to a different playfab account!");
Debug.Log(_playfabIds[0].PlayFabId);
}
}
}, error =>
{
Debug.Log("failed to retrieve any attached playfab accounts " + error.GenerateErrorReport());
});
}
else
{
Debug.LogError("Failed to login to facebook " + result.Error);
}
}
public void Unlink()
{
FB.LogOut();
PlayFabClientAPI.UnlinkFacebookAccount(new UnlinkFacebookAccountRequest(), result =>
{
Debug.Log("Unlinked facebook account.");
UpdateButtonStatus();
}, error =>
{
Debug.LogError("Failed to unlink facebook account! " + error.GenerateErrorReport());
});
}
public void LinkAccount(bool forceLink, Action<bool> onComplete)
{
PlayFabClientAPI.LinkFacebookAccount(new LinkFacebookAccountRequest
{
AccessToken = AccessToken.CurrentAccessToken.TokenString,
ForceLink = forceLink,
}, accountResult =>
{
PlayFabLogin.LoginResult.InfoResultPayload.AccountInfo.FacebookInfo = new UserFacebookInfo
{
FacebookId = AccessToken.CurrentAccessToken.UserId
};
Debug.Log("Linked account successfully");
onComplete?.Invoke(true);
}, error =>
{
Debug.Log("Failed to link facebook account! " + error.GenerateErrorReport());
onComplete?.Invoke(false);
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment