question

DreamDelerium avatar image
DreamDelerium asked

Authentication with Facebook Odd Behavior

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();
}
}

}

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sarah Zhang avatar image Sarah Zhang commented ·

Could you please clarify how you defined the PlayFabAuthService? Or do you mean you used our authentication sample code? Could you please provide the corresponding sample code or the URL for our reference?

0 Likes 0 ·

1 Answer

·
DreamDelerium avatar image
DreamDelerium answered

@Sarah Zhang the issue turned out to be that I had unity setup for quick play. I guess this keeps some code (static references) cached. So, every time I ran my code, it was just compounding the addition of events. What I did was use OnDestroy to unregister my events and that fixed the issue.

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sarah Zhang avatar image Sarah Zhang commented ·

Glad to know you solved the issue, and thanks for your sharing.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.