question

mirko avatar image
mirko asked

Allow only one session per user

Hello everyone,

thought some of you might encounter this problem so I wrote simple solution for it.

Problem:

If user is logged in and active from multiple devices at the same time, it can cause desync issues with the cloud save files where both devices are uploading their own save data.

Fix idea:

To prevent this issue occurring and possibly having corrupted save data or complete save data mismatch(if devices are playing on different save slots) I figure out it would be good only to allow latest logged device to sync data with cloud.

This solution steps are written for unity, but should work on different platforms.

How to:

Unity posses this extra cool function that goes by: SystemInfo.deviceUniqueIdentifier which generates unique id for every device and it generates that its always the same. You can also use GUID/Dates or something similar it doesn't really matter as long as its almost unique.

After you acquire unique ID you update it to player data using:

PlayFabClientAPI.UpdateUserData(userDataRequest, success, failed);


Picture above displays the key I used and value I generated.

I usually add that block of code after automatic or manual login so that it updates data after login was successful.

Next step is to inject yourself somewhere before you request file upload to check if your current session on device is the last one. You can do that by calling

PlayFabClientAPI.GetUserData(userDataRequest, success, failed);

Where you send "CurrentSessionDeviceID" as a required key. I wrote example function under, that should fire success action or failed depending on session key match.

    private void CheckIfSessionIsStillValid(Action success, Action<PlayFabError> failed)
    {
        GetUserData(new string[] { CURRENT_SESSION_DEVICE_ID }, 
            resultSuccess =>
            {
                if(resultSuccess.Data.ContainsKey(CURRENT_SESSION_DEVICE_ID))
                {
                    var lastSessionDeviceId = resultSuccess.Data[CURRENT_SESSION_DEVICE_ID].Value;


                    if(lastSessionDeviceId == SystemInfo.deviceUniqueIdentifier)
                    {
                        success();
                    }
                    else
                    {
                        failed( new PlayFabError() { Error = PlayFabErrorCode.ExpiredAuthToken });
                    }
                }
                else
                {
                    success();
                }
            },
            resultFailed =>
            {
                Debug.LogError(resultFailed.ToString());
            });
    }

If key doesnt exist I assume there is no active session. If lastSessionDeviceId is equal to SystemInfo.deviceUniqueIdentifier then it means this session is the last active one so its a success. If there is a mismatch it means its a fail.

Hopefully someone will find this useful.

Kind regards!

1lqpp.png (8.2 KiB)
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

·
Citrus Yan avatar image
Citrus Yan answered

Thanks for sharing your insights with this community:)

10 |1200

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

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.