Hello. I am trying to implement logging in via Facebook, in Unity. But, I am experiencing and odd issue, which I am not sure if it is part of the normal workflow or not. When my app first starts up (and has never authenticated before) the OnDisplayAuthentication method gets called multiple times. The number of times it gets called equals the number of times I have run the app. So, I press play, an OnDisplayAuthentication will get called twice in a row. If I stop Unity and restart it, it will get called three times. If I stop and start it again, it will get called four times and so on.
Here is my code:
public class PlayFabLogin : MonoBehaviour
{
public void Start()
{
PlayFabAuthService.OnDisplayAuthentication += OnDisplayAuthentication;
PlayFabAuthService.FBOnLoginSuccess += OnFBLoginSuccess;
PlayFabAuthService.OnPlayFabError += OnPlayFabError;
FB.Init(OnFBInitComplete, OnFBHideUnity);
_authService.InfoRequestParams = InfoRequestParams;
_authService.RememberMe = true;
_authService.Authenticate();
}
public void LoginByFaceBookButtonClick()
{
FB.LogInWithReadPermissions(new List<string>() { "public_profile", "email", "user_friends" }, OnHandleFBResult);
}
private void OnPlayFabError(PlayFabError error)
{
//handler error
}
private void OnDisplayAuthentication()
{
if (FB.IsInitialized)
{
if (AccessToken.CurrentAccessToken == null || !FB.IsLoggedIn)
{
Debug.Log("No Access Token");
}
else
{
_authService.AuthTicket = AccessToken.CurrentAccessToken.TokenString;
_authService.RememberMe = true;
_authService.Authenticate(Authtypes.Facebook);}
}
}
public void OnHandleFBResult(ILoginResult result)
{
if (result.Cancelled)
{
Debug.Log("Facebook Login Cancelled");
}
else if (result.Error != null)
{
Debug.LogError(result.Error);
}
else
{
_authService.AuthTicket = result.AccessToken.TokenString;
_authService.Authenticate(Authtypes.Facebook);
}
}
private void OnFBLoginSuccess(PlayFab.ClientModels.LoginResult result)
{
Debug.Log("Success");
}
private void OnFBHideUnity(bool isUnityShown)
{
Debug.Log("OnFBHide");
}
private void OnFBInitComplete()
{
if (AccessToken.CurrentAccessToken != null)
{
_authService.AuthTicket = AccessToken.CurrentAccessToken.TokenString;
_authService.Authenticate(Authtypes.Facebook);
}
else
{
IsFacebookLoggedin = false;
}
}
public void Logout()
{
_authService.UnlinkSilentAuth();
_authService.ClearRememberMe();
_authService.AuthType = Authtypes.None;
if (FB.IsInitialized)
{
FB.LogOut();
}
}
}