question

laurentiumarianivan avatar image
laurentiumarianivan asked

Unique account per device

Hello,

How would I go about making sure that only one account / device is allowed? I was thinking of linking the device id to LoginWithEmailAndPassword after registration, but I am not exactly sure if it is the best way. Any help is appreciated.

Thanks!

Account ManagementAuthentication
10 |1200

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

Rick Chen avatar image
Rick Chen answered

You can use LinkAndroidDeviceID or LinkIOSDeviceID on your successful callback of LoginWithEmailAddress.

You may refer to the code snippet below, don’t forget to import the PlayFab SDK.

If the device id has been linked to another account, you will receive error message "That device is already linked to a different user" in the error callback. You can write your own error callback handler to replace to null callback below.

public void LoginWithEmailAndPassword(string email, string password)
    {
        PlayFabClientAPI.LoginWithEmailAddress(new LoginWithEmailAddressRequest()
        {
            TitleId = PlayFabSettings.TitleId,
            Email = email,
            Password = password,
            InfoRequestParameters =  new GetPlayerCombinedInfoRequestParams { GetUserAccountInfo = true}
        },
        OnLoginSuccessful,
        null //your error callback here
        );
    }
    public void OnLoginSuccessful(LoginResult login_result)
    {
                //if account is not linked to an android device
                if (!string.IsNullOrEmpty(login_result.InfoResultPayload.AccountInfo.AndroidDeviceInfo.AndroidDeviceId))
                {
#if UNITY_ANDROID && !UNITY_EDITOR
       
                    PlayFabClientAPI.LinkAndroidDeviceID(new LinkAndroidDeviceIDRequest()
                    {
                        AndroidDevice = "Nexus 6",
                        AndroidDeviceId = SystemInfo.deviceUniqueIdentifier,
                        OS = "5.0"
                    },
                    (LinkAndroidDeviceIDResult result)=>{
                        Debug.Log("Link successful");
                    },
                    null // your error callback here
                    ) ;
#endif
                }
                //if account is not linked to an ios device
                if (!string.IsNullOrEmpty(login_result.InfoResultPayload.AccountInfo.IosDeviceInfo.IosDeviceId))
                {
#if UNITY_IPHONE || UNITY_IOS && !UNITY_EDITOR
                    PlayFabClientAPI.LinkIOSDeviceID(new LinkIOSDeviceIDRequest()
                    {
                        DeviceId = SystemInfo.deviceUniqueIdentifier,
                        OS = "7.11",
                        DeviceModel = "Iphone 5s"
                    },
                    (LinkIOSDeviceIDResult result) =>{
                        Debug.Log("Link successful");
                    },
                    null // your error call back here
                    );
#endif
                }
}
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.

laurentiumarianivan avatar image laurentiumarianivan commented ·

This is exactly what I needed, thanks a lot!

0 Likes 0 ·
Rick Chen avatar image
Rick Chen answered

If you are looking for a way to implement devices sharing same account, I would suggest you to go through this document: Add new devices to an existing recoverable account. If you have further questions, please feel free to ask.

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.

laurentiumarianivan avatar image laurentiumarianivan commented ·

Thanks for your fast answer! I actually want it so that an account is tied to a device.

The flow will be something like: the user creates an account with email and password, then I link the device ID to this newly created account so that the user can have only 1 account per device.

0 Likes 0 ·
Rick Chen avatar image Rick Chen ♦ laurentiumarianivan commented ·

May I ask which platform or language are you using?

0 Likes 0 ·
laurentiumarianivan avatar image laurentiumarianivan Rick Chen ♦ commented ·

Oh, sorry, I forgot to mention. I am using Unity and C#, and the game is meant to be for Android and iOS.

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.