question

corporate avatar image
corporate asked

How to avoid duplicate LoginWithCustomID & LoginWithIOSDevice logins?

Hi there -

First-time PlayFab user switching from Braincloud chiefly because of this support community :)

I am working in Unity on a mobile game where some back-end features need to be accessible to all users (for example, login time and global variables) while others are reserved for Facebook-authenticated users only (for example, leaderboards).

Currently, I authenticate from the Start() function, like so:

void Start ()
{
	var request = new LoginWithIOSDeviceIDRequest { DeviceId = SystemInfo.deviceUniqueIdentifier, CreateAccount = true, TitleId = "XXX" };
	PlayFabClientAPI.LoginWithIOSDeviceID(request, PlayFabLoginSuccess, PlayFabLoginError, null, null);

}

I then have another script that checks whether the user has logged in with Facebook and, if so, authenticates with PlayFab using Facebook credentials:

private void Start()
        {
        if (!FB.IsInitialized)
            {
            FB.Init(onInitComplete: FBInitCallBack);
            }
        else
            {
            FB.ActivateApp();
            }
        }

oid FBInitCallBack()
        {
        FB.ActivateApp();
        if (FB.IsLoggedIn)
            {
            OnFacebookLogin();
            AuthenticateBCWithFacebook();
            }
        }

public void AuthenticateBCWithFacebook()
        {
	var request = new LoginWithFacebookRequest { AccessToken = AccessToken.CurrentAccessToken.TokenString, TitleId = "F248", CreateAccount = true };
        PlayFabClientAPI.LoginWithFacebook(request, null, null, null, null);
        }

Unfortunately, this leads to situations where PlayFab treats the same user as two different users - one who authenticated with their DeviceID and another who authenticated with their Facebook account.

How do I avoid this duplication? I've been thinking about simply checking whether the user is logged into Facebook first and, if so, authenticating with Facebook and, if not, authenticating with the device ID - would that solve it?

unity3dAuthentication
10 |1200

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

1 Answer

·
brendan avatar image
brendan answered

What you really need to do is use the Link... API call to connect additional login credentials to the original user account, so that when you sign a player in, regardless of how you do so, you're logging them into the same account. I'd start with this tutorial, which walks you through our recommendations on best practices around account management: https://api.playfab.com/docs/tutorials/landing-players/best-login

3 comments
10 |1200

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

corporate avatar image corporate commented ·

Thank you @Brendan! Will do so when I get home tonight and update accordingly. I'm loving the level of support here already!

0 Likes 0 ·
corporate avatar image corporate commented ·

Hi Brendan -

As promised, I'm writing back to let you know that everything's up and running swimmingly. For others, here's the workflow I used:

  1. Initiate Facebook (FB.Init()) in the Awake function and put subsequent PlayFab authentication into its success callback
  2. Check if logged into a PlayFab account. If not, check if logged into Facebook (FB.IsLoggedIn) and, if yes, proceed to authenticate via Facebook (PlayFabClientAPI.LoginWithFacebook)
  3. If not logged into Facebook, fall back onto using device IDs for authentication - use #IF UNITY_IOS and #IF UNITY_ANDROID for conditional compilation and call LogInWithIOSDevice / LogInWithAndroidDevice respectively
  4. Put the appropriate link function into the callback of your preferred authentication method (e.g., LinkIOSDeviceIDRequest if you're authenticating with an iOS device ID, LinkFacebookAccountRequest if you're authenticating via Facebook, etc). This step prevents duplicate player accounts. Basically, accounts get linked as soon as they get authenticated.

@Brendan - is it OK to call the link function at the end of every authentication request, even if the accounts are linked? I expected it to raise the "already linked" error but it doesn't.

0 Likes 0 ·
brendan avatar image brendan corporate commented ·

If you're re-linking to the same account, it re-updates the Facebook record on our side with the Facebook user's name, so that would update it if they changed it. But yes, it doesn't do any real unlink/relink in that case, so it's not a problem.

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.