-
-
Save xjjon/8f3b830fb556b6f9b8b0e43439aa9e71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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